2017-04-25 37 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以外的东西。

相关问题