我試圖讓用戶輸入一個特定的單詞。Python:檢查用戶對raw_input的迴應()
我的代碼:
import os
os.system("clear")
def get_answer():
print "\nWould you like to 'hit', 'stick', 'double' down or 'split'?"
x = raw_input('> ')
answers = ['hit', 'stick', 'double', 'split']
y = [i for i in answers if i in x]
if y == []:
get_answer()
print y
# exit(0)
return y
def foo():
a = get_answer()
print a
foo()
這裏是我的輸出,如果我回答「打」的第一次;
Would you like to 'hit', 'stick', 'double' down or 'split'?
> hit
['hit']
['hit']
這裏是我的輸出,如果我輸入「嗒嗒」拳頭時間,然後「重災區」:
Would you like to 'hit', 'stick', 'double' down or 'split'?
> blah
Would you like to 'hit', 'stick', 'double' down or 'split'?
> hit
['hit']
[]
[]
我甚至不真正懂得研究這個。這是一個簡單的語法錯誤還是有一個更深的問題,我只是不明白?我很想知道如何正確地做到這一點。
的['cmd'(http://docs.python.org/library/cmd.html)模塊可能是你想要什麼 – JBernardo