2016-04-27 55 views
0

所以,我似乎有我的選擇4的問題。我想它回到主菜單,可以通過main()調用。 「是」顯然起作用,當輸入除「是」或「是」以外的任何內容時,都應將其返回到主菜單。在「否」或「hhh」中鍵入只是退出程序以及「是」/「是」。它也顯示了與「是」一樣的「謝謝演奏」的信息。我是Python的初學者,所以請忍受我在這裏。做,雖然,菜單選擇退出工作不正確

while endProgram == "Yes" or "yes": 

    #Selection menu, user input to navigate. 
    selection = eval(input("Your selection: ")) 

    #Selection 1, rules. 
    if selection == 1: 
     rpsRules() 
     returnMain = input("\nWhen you would like to return to the Main Menu, press Enter.") 
     main() 

    #Selection 2, begin a match against the PC, calls againstPC module with choice as an argument. 
    elif selection == 2: 
     againstPC(choice) 

    #Selection 3, begin a match against another player locally, calls twoPlayer module. 
    elif selection == 3: 
     twoPlayer() 

    #Selection 4, end program, with an "Are you sure?" catch.  
    elif selection == 4: 
     endProgram = input("\nAre you sure you want to quit? (Yes/No) ") 
     if endProgram == "Yes" or "yes": 
      print("\nThanks for playing Rock, Paper, Scissors!\nSee you next time!") 
      break 
     elif endProgram == "No" or "no": 
      main() 
     else: 
      main() 
    elif selection == 5: 
     creatorCredits() 
+0

'endProgram ==「是」或「是」'始終是真實的,因爲你正在檢查'如果'是「','」是「'不是'假',你的程序永遠不會結束。 – user312016

+0

你在選擇4中調用main()函數時是否有任何代碼?如果沒有,那可能是你的程序之後退出的原因。另外,你希望你的IF語句看起來像這樣:if endProgram ==「Yes」or endProgram ==「yes」除此之外,它不一定回答你的問題,但在你的第一個IF語句之後,看看字符串是「是」還是「是」,你可以只有一個人。無論他們輸入「否」還是「等等等等」都不重要,因爲您的ELIF和ELSE都會對main進行回調。 –

+0

您需要將'x ==「a」或「b」'更改爲'x in(「a」,「b」)'來獲得所需的效果。否則,表達式將始終爲真,因爲它像「(x ==」a「)或」b「這樣的組合,這顯然總是成立的。 –

回答

0

答案是改變你的IF語句:

if endProgram == "Yes" or endProgram == "yes": 
... 
1

這條線:

while endProgram == "Yes" or "yes" 

李四並不意味着當你用英語讀出來,它說什麼;你需要修改它,所以它針對endProgram檢查這兩個值,就像這樣:

while endProgram == "Yes" or endProgram == "yes" 

然後,您可以進一步使其更簡單說:

while endProgram.lower() == "yes" 

你應該永遠不會使用eval用戶輸入。這不僅是危險的,而且是錯誤的重要來源,它可能會導致應用程序出現不可預知的行爲。

事實上,你應該把這個邏輯在main()方法,你在哪裏打印菜單:

def main(): 
    # print the menu here 
    selection = input("Your selection: ") 
    try: 
     selection = int(selection) 
    except ValueError: 
     print('Please enter a valid number') 
     get_user_input() 
    return selection 

然後,把它在你的主,而:

while endProgram.lower() == "yes": 
    selection = main() 

下一個問題你有這個部分,它和while循環做同樣的檢查;這是不必要的。

elif selection == 4: 
    endProgram = input("\nAre you sure you want to quit? (Yes/No) ") 
    if endProgram == "Yes" or "yes": 
     print("\nThanks for playing Rock, Paper, Scissors!\nSee you next time!") 
     break 
    elif endProgram == "No" or "no": 
     main() 
    else: 
     main() 

要清理這一切了,結構你的程序是這樣的:

  1. 打印主菜單。
  2. 儘管主菜單中的選項不會退出,但請運行循環。
  3. 循環中的每次選擇後,再次打印主菜單。

由於您的主菜單功能返回有效的響應;你可以在那裏移到你的出口邏輯,以及:

def main(): 
    # print your menu here 
    selection = input('Please enter your choice: ') 
    try: 
     selection = int(selection) 
    except ValueError: 
     print('Sorry, {} is not a valid choice'.format(selection)) 
     main() 
    if selection == 4: 
     exit_check = input('Are you sure you want to exit? Type Yes: ') 
     return exit_check.lower() 
    if 0 < selection < 4: 
     print('{} is not a valid menu item.') 
     main() 
    return selection 

現在,你的程序的主要邏輯循環是這樣的:

end_program = main() 
while end_program != 'yes': 
    if end_program == 1: 
     # do stuff 
     end_program = main() 
    if end_program == 2: 
     # do stuff 
     end_program = main() 
print('Thank you, for playing. Good bye!')