我试图让用户输入一个特定的词。
我的代码:
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']
如果我第一次输入“blah”然后“hit”,这是我的输出:
Would you like to 'hit', 'stick', 'double' down or 'split'?
> blah
Would you like to 'hit', 'stick', 'double' down or 'split'?
> hit
['hit']
[]
[]
我什至不知道如何研究这个。这是一个简单的语法错误还是我不明白的更深层次的问题?我很想知道如何正确地做到这一点。