Reputation: 450
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)
Upvotes: 1
Views: 1100
Reputation: 41
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 = ...
Upvotes: 2
Reputation: 431
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.
Upvotes: 2
Reputation: 4905
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.
Upvotes: 2