错误与Elif预期的缩进块
问题描述:
我想为我的应用程序创建一个菜单,菜单有4个选项,并且这些选项中的每一个都应该在用户输入所选值时返回正确的信息。我不断收到Elif报表的错误。 我是一个新手,所以请理解从哪里来。 非常赞赏。错误与Elif预期的缩进块
当我缩进while回答:我会收到一个错误说,无效的语法缩进elif ans == 2后。
elif ans == 2 < ---当我缩进时,此错误一直说indention块错误或syntex无效。
DEF print_menu(个体,汽车):
打印( “1.Search由platenumber”) 打印( “2.Search由价格 ”) 打印(“ 3.删除3”) 打印(“4 .Exit 4 “)
循环=真
while循环: print_menu() ANS ==输入(” 请从列表中选择“)
if ans==1:
print("These are the cars within this platenumber")
return platenumber_
while ans:
if ans==2:
elif ans==2:
print("These are the prices of the cars")
return price_
elif ans==3:
print("Delete the cars ")
return delete_
elif ans==4:
return Exit_
loop=False
else:
raw_input("please choose a correct option")
答
你有没有身体的3210循环。一般来说,如果存在缩进错误消息,并且错误不在所提到的行上,那么它就位于它的上方。
loop=True
while loop:
print_menu()
ans = int(input("Please choose from the list"))
if ans==1:
print("These are the cars within this platenumber")
# return some valid information about plate numbers
elif ans==2:
print("These are the prices of the cars")
# return some valid information about pricing
elif ans==3:
print("Delete the cars ")
# Perform car deletion action and return
elif ans==4:
# I am assuming this is the exit option? in which case
# return without doing anything
else:
# In this case they have not chosen a valid option. Send
# A message to the user, and do nothing. The while loop will run again.
print("please choose a correct option")
此外,你的代码有点让我困惑。无论如何,看起来你要去return car_
,这意味着你的循环只会执行一次。另外,=
是赋值,==
是相等的。小心。
你有'while ans:',之后没有缩进。如果你用':'结束了一行,那么当我缩进之后,我应该有一个缩进 – Craicerjack
我收到一个错误,说无效的语法 – Sam
从你的问题开始,尽管它很难理解工作中的逻辑,你的'return'语句很奇怪缩进,并且你的'if'语句中间有'while'语句。你为什么首先在“while”循环中? – Craicerjack