2013-08-23 48 views
0

我想要做的是向用戶提問(用戶輸入)(答案:是,否),如果他們說是,有事情發生,如果他們說不,還有其他事情發生。我如何做用戶輸入

I.E: 我問用戶他們是否想要比他們的計算機可以打字速度快。如果他們說是,我們有一場比賽,如果他們說不,我可以繼續其他事情。

回答

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!') 
+1

我看不錯。任何使用Python 2.x的人都應該使用'raw_input()'。 – vroomfondel

+0

我該如何做到這一點,如果你選擇是的,它會啓動一些東西,但如果你選擇否,就會啓動其他的東西。例如,如果你說「是」,你有一個打字比賽,如果你說「不」,你會跳過這個並進入下一件事。 – user2712431

+1

@ user2712431請**閱讀**和**使用您的大腦**答案*只是告訴您如何去做。* – 2013-08-23 23:39:49

相關問題