Seanosapien Seanosapien - 25 days ago 11
Python Question

Syntax errors for elif statement in Python...?

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)

Answer

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.

Comments