我的while循环不适用于输入验证

时间:2019-12-11 16:00:32

标签: python validation if-statement while-loop

end_program = False
while end_program == False:
    user_choice = input('\nWould you like to play a game?\n Please pick yes or no\n').lower()
    if user_choice != 'yes' or 'no':
        print('You didn\'t pick yes or no...try again please\n')
        end_program = False
    else:
        end_program = True

1 个答案:

答案 0 :(得分:0)

尝试一下:

end_program = False
while end_program == False:
    user_choice = input('\nWould you like to play a game?\n Please pick yes or 
    no\n').lower()
    if user_choice != 'yes' or user_choice !='no':
       print('You didn\'t pick yes or no...try again please\n')
       end_program = False
    else:
       end_program = True

在检查多个条件时,您需要具体说明: 你不能说: if user_choice!= 'yes' or 'no' 在此处使用“否”会导致错误。您将需要单独检查条件:

if user_choice != 'yes' or user_choice != 'no':