2017-04-25 40 views
0

我有一個輸入,用戶只能做出三種可能的選擇,而我目前正在嘗試調試它,但現在沒有運氣。我試着用數字值的方式嘗試,但它沒有奏效,現在不會讓我輸入任何內容。我也嘗試看看我是否可以對變量(值)執行user_choice!=,但是我的輸出和以前一樣。在Python中僅用3個答案來調試選擇

while loser != 'Lose': 
    key_error = True 
    while key_error: 
     try: 
      user_choice = str(input('Enter your choice: ')) 
      user_choice = (user_choice.lower()) 
      if user_choice != 'r' or user_choice != 'p' or user_choice != 's': 
       print(" Please enter either r, s or p") 
      else: 
       key_error = False 
     except ValueError: 
      print(" Invalid input, Please enter either r, s or p: ") 

回答

0

if user_choice != 'r' or user_choice != 'p' or user_choice != 's':永遠是正確的,因爲它不能被所有三個一次,而這一切花費是or部分爲一個if語句是真實的。試試這個:

if user_choice not in ('r', 'p', 's'):

+0

非常感謝。 –

+0

是的,我明白你的意思。我不知道不在功能,謝謝你教給我。 –

0
while loser != 'Lose': 
     while True: 
      user_choice = str(input('Enter your choice: ')) 
      user_choice = (user_choice.lower()) 
      if user_choice != 'r' and user_choice != 'p' and user_choice != 's': 
       print(" Please enter either r, s or p") 
      else: 
       break 

這將繼續執行程序只有當用戶輸入了比R,S或P以外的東西。

相關問題