2014-01-06 93 views
2

我所做的Python程序是基於菜單的,需要用戶輸入來瀏覽程序。Python:如何在循環中結束程序?

我已經放置了一個while循環,以便使用戶回到開始菜單以執行任務,但其中一個選項是「按0退出」將只是重新啓動循環而不結束程序。下面是代碼:

terms = {"ALU":"Arithmetic Logic Unit", "CPU":"Central Processing Unit", "GPU":"Graphics Processing Unit"} 
while True: 
    print(
    """ 
    Computing Terminology 

    0 - Quit 
    1 - Look Up a Term 
    2 - Add a Term 
    3 - Redefine a Term 
    4 - Delete a Term 
    5 - Display All Terms 
    """ 
    ) 
    menu = input("Choice: ") 
    print() 
    while menu != "0": 
     if menu == "1": 
      print("\n") 
      term = input("Type in a term you wish to see: ") 
      if term in terms: 
       definition = terms[term] 
       print("\n") 
       print(term, "means", definition, "\n") 
       break 
      else: 
       print("This term does not exist.\n") 
       break 
     elif menu == "2": 
      term = input("What term would you like to add?: ") 
      if term not in terms: 
       print("\n") 
       definition = input("What's the definition?: ") 
       terms[term] = definition 
       print("\n") 
       print(term, "has been added.\n") 
       break 
      else: 
       print("\n") 
       print("Term already exists, try redefining it instead.\n") 
       break 
     elif menu == "3": 
      term = input("Which term do you want to redefine?: ") 
      if term in terms: 
       definition = input("What's the new definition?: ") 
       terms[term] = definition 
       print("\n") 
       print(term, "has been redefined.\n") 
       break 
      else: 
       print("\n") 
       print("That term doesn't exist, try adding it instead.\n") 
       break 
     elif menu == "4": 
      term = input("Which term would you like to delete?: ") 
      if term in terms: 
       del terms[term] 
       print("\n") 
       print("The term has been deleted.\n") 
       break 
      else: 
       print("\n") 
       print("This term doesn't exist.\n") 
       break 
     elif menu == "5": 
      print("\n") 
      print("Terms available are: ") 
      for term in terms: 
       print("\n", term, "\n") 
     else: 
      print("\n") 
      print("Sorry, but", menu, "is not a valid choice.\n") 
      break 

print("\n") 
input("Press any key to exit.") #if user enters 0 I want the program to end here. 
+0

它可能是個人喜好,但我會避免使用字符串輸入,如果選擇將是數字。對我來說,讓它們成爲「int」是更有意義的,以避免對你是否需要引用行爲引起混淆。 – IanAuld

+0

@IanAuld公平點。 – RoyalSwish

回答

3

這是我經常做

def print_menu(): 
    print('What would you like to do: ') 
    print('1. Retrieve data') 
    print('2. Store Data') 
    print('3. Exit') 

while True: 
    print_menu() 
    choice = int(input('Enter a number: ')) 
    if choice == 1: 
     #blah 
    elif choice == 2: 
     #blah blah 
    elif choice == 3: 
     break 
+3

它的作品 - 但貫穿所有如果每次 - 使用elif – volcano

+0

感謝您的指針。現在更新代碼。 – IanAuld

3

menu = input("Choice: ")內while循環

+0

我這樣做了,但是當我運行它時,它只會出現一個'Traceback'錯誤。然後,我在第一個'while'循環中添加了'menu = None',程序將運行,但只有在「0」纔有響應'「對不起,但0不是有效的選擇。 – RoyalSwish

+0

如果你想'菜單'等於和空字符串,你可以使用'menu =''' – IanAuld

0

也許我太快略讀你的代碼,但我沒有看到外while True循環的地步。但是,退出程序的好方法是使用sys.exit

+0

嗨,在這個問題到來之前,我搜索瞭如何讓我的程序循環回到開始菜單,而不是退出以使它對最終用戶更有用。這個雙While循環是我發現的一個建議,它的工作原理,但造成了這篇文章討論的問題。 – RoyalSwish

+0

不知道我同意雙循環,但如果它適合你,'sys.exit'可能是你最好的選擇。 IanAuld的答案不使用雙循環,是最好的整體解決方案。 – aquavitae

+0

是的,我已經改變爲伊恩的答案,因此它被標記爲我的問題的解決方案。我完全忘了定義一個函數。這是我[建立](http://stackoverflow.com/questions/14907067/how-do-i-restart-a-program-based-on-user-input)爲雙循環的建議。 – RoyalSwish