2017-03-31 53 views
-3

我想爲我的應用程序創建一個菜單,菜單有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") 
+0

你有'while ans:',之後沒有縮進。如果你用':'結束了一行,那麼當我縮進之後,我應該有一個縮進 – Craicerjack

+0

我收到一個錯誤,說無效的語法 – Sam

+2

從你的問題開始,儘管它很難理解工作中的邏輯,你的'return'語句很奇怪縮進,並且你的'if'語句中間有'while'語句。你爲什麼首先在「while」循環中? – Craicerjack

回答

0

你有沒有身體的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_,這意味着你的循環只會執行一次。另外,=是賦值,==是相等的。小心。

+0

我應該刪除elif並添加如果陳述後的時間:你會推薦什麼? – Sam

+0

查看更新的代碼。 –

+0

謝謝我會繼續努力,謝謝您的反饋 – Sam