Elif预期缩进块

时间:2017-03-31 16:06:49

标签: python if-statement

我尝试为我的应用程序创建一个菜单,菜单有4个选项,当用户输入所选值时,每个选项都应返回正确的信息。我一直在使用Elif语句出错。 我是新手,所以请了解我的来源。 非常欣赏。

当我缩进while ans时:我会收到一个错误,说明在缩进elif ans == 2之后语法无效。

elif ans == 2< ---当我缩进时,这个错误一直说缩进块错误或syntex无效。

def print_menu(self,car):
            打印(" 1.按platenumber搜索")             打印(" 2.按价格搜索")             打印(" 3.删除3")             打印(" 4.退出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")

1 个答案:

答案 0 :(得分:0)

你有一个没有身体的while循环。一般来说,如果存在缩进错误消息并且错误不在所提及的行上,那么它就会紧跟在它之上。

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_,这意味着你的循环只会执行一次。此外,=是赋值,==是相等的。小心。