1

I am getting a syntax error every time I run this code. I don't understand why as elsewhere in my script, I use the same structure and it works fine.

I get a syntax error for the elif region == 2: line first. Then, I get a load of indentation errors. I've played around with the indentations to no avail. Here's hoping someone can spot the problem.

Thanks in advance.

def main():

back2main = "y"

while back2main == "y":

    print("Main Menu");print("1.)Highest Rainfall in one Day");print("2.)Wettest Location in Ireland");print("3.)Average Monthly Raindays");print("4.)[Construct Unique Query]");print("5.)Exit")
    choice = input("Please select one of options 1:5 above:")

    if choice == 1:

        print("1.)Cork");print("2.)Belfast");print("3.)Dublin");print("4.)Galway");print("5.)Limerick")
        region = input("Please enter a city from the numbered list above:")

        if region == 1:

            corkRain = open("CorkRainfall.txt","r")

            highestRain = 0.0

            for line in corkRain:
                data = line.split(" ")
                if float(data[3]) > highestRain:
                    highestRain = float(data[3])

        print("Highest rainfall in a single day for Cork: " + str(highestRain) + " mm")

        corkRain.close()

        back2main = raw_input("Return to Main Menu? (y/n):")

        elif region == 2:

            belfastRain = open("BelfastRainfall.txt","r")

            highestRain = 0.0

            for line in belfastRain:
                data = line.split(" ")
                if float(data[3]) > highestRain:
                    highestRain = float(data[3])

        print("Highest rainfall in a single day in Belfast: ") + str(highestRain)
3
  • 1
    Your indentation is wrong... I suspect the 3 lines of code before the elif need to be indented. Or the whole elif block needs to be dedented. Commented Nov 9, 2016 at 20:14
  • 1
    Your elif statement doesn't seem to be connected to any if statement. Maybe you meant to indent the three lines before it? Commented Nov 9, 2016 at 20:14
  • I literally just figured it out after I posted the question. Newbie error. Thanks all. Commented Nov 9, 2016 at 20:15

3 Answers 3

2

The elif block has to be on the same position, vertically, as the if block above it. That means all the code under elif, including elif should be moved by one Tab to the left.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I've been writing this program for two days. 600+ lines have the eyes gone weird. Time for a tea break.
You get used to things like that and notice them immediately, after some time.
Thanks again @yper.
2

Your problem is here:

print("Highest rainfall in a single day for Cork: " + str(highestRain) + " mm")
corkRain.close()
back2main = raw_input("Return to Main Menu? (y/n):")

These lines should be one more level indented if you want them to be under the first if.

On execution is interpreted as out of the previous scope, and either next one is "if" instead of "elif" or if you want to belong to previous if it should be one indentation right.

1 Comment

Thanks @maki. Sorted.
2

I would recommend trying this:

def main():

    back2main = "y"

    while back2main == "y":

        print("Main Menu\n1.)Highest Rainfall in one Day\n2.)Wettest Location in Ireland\n3.)Average Monthly Raindays\n4.)[Construct Unique Query]\n5.)Exit")
        choice = input("Please select one of options 1:5 above:")

        if choice == 1:

            print("1.)Cork\n2.)Belfast\n3.)Dublin\n4.)Galway\n5.)Limerick")
            region = input("Please enter a city from the numbered list above:")

            if region == 1:

                corkRain = open("CorkRainfall.txt","r")

                highestRain = 0.0

                for line in corkRain:
                    data = line.split(" ")
                    if float(data[3]) > highestRain:
                        highestRain = float(data[3])



            elif region == 2:

                belfastRain = open("BelfastRainfall.txt","r")

                highestRain = 0.0

                for line in belfastRain:
                    data = line.split(" ")
                    if float(data[3]) > highestRain:
                        highestRain = float(data[3])

            print("Highest rainfall in a single day for Cork: " + str(highestRain) + " mm")

            corkRain.close()

            back2main = raw_input("Return to Main Menu? (y/n):")

            print("Highest rainfall in a single day in Belfast: ") + str(highestRain)

You can replace the multiple print statements with new line breaks (The \n) and all of the code below the first line needed to be indented. Also, the elif was breaking because of the lines:

print("highest rainfall...
corkRain.close()
back 2main = ...

1 Comment

Cheers!! Yes. The lines below aren't in the correct order. I managed to get everything running smoothly after a quick break. Thanks for the tip on the multiple print() statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.