gpt4 book ai didi

python - Python 中 elif 语句的语法错误...?

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:52 24 4
gpt4 key购买 nike

每次运行此代码时,我都会遇到语法错误。我不明白为什么与我的脚本中的其他地方一样,我使用相同的结构并且工作正常。

我得到 elif 区域的语法错误 == 2: 第一行。然后,我收到一大堆缩进错误。我玩过缩进无济于事。希望有人能发现问题。

提前致谢。

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)

最佳答案

elif block 必须与其上方的 if block 在同一垂直位置。这意味着 elif 下的所有代码,包括 elif 都应该向左移动一个 Tab。

关于python - Python 中 elif 语句的语法错误...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com