在Python中只有3个答案调试选择



我有一个输入,只有三个可能的选择可以做出,我当前正在尝试在没有运气的情况下对其进行调试。我尝试使用数字值完成的方式,但是它尚未工作,并且不会让我现在完全键入任何内容。我还试图看看我是否可以执行user_choice!=对变量(值(,但是我的输出与以前相同。

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: ")

if user_choice != 'r' or user_choice != 'p' or user_choice != 's':将永远是正确的,因为它不能一次是三个,而它所需的只是 or零件之一,如果语句为true。尝试以下操作:

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

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以外的其他内容时才继续执行您的程序。

最新更新