我该如何进行用户输入

时间:2013-08-23 23:02:36

标签: python-3.x

我想做的是向用户提出问题(用户输入)(答案:是,否)如果他们说是,有事情发生,如果他们说不,就会发生其他事情。

即: 我问用户他们是否想要与计算机相比,他们可以选择多快的速度。如果他们说是,我们就有一场比赛,如果他们说不,我可以继续其他的。

1 个答案:

答案 0 :(得分:6)

您要找的是input

user_input = input('Yes or No?: ')
if user_input == 'Yes':
    print('You said yes!')
elif user_input == 'No':
    print('You said no!')
else:
    print('You said neither.')

如果您想确保用户输入“是”或“否”的用户,则可以执行以下操作:

while True:
    user_input = input('Yes or No?: ')
    if user_input in ['Yes', 'No']:
        break
    else:
        print('That is not a valid option!')

if user_input == 'Yes':
    print('You said yes!')
else:
    print('You said no!')
相关问题