我的问题是:假设我们在Python上有提示,并且我们需要比较提示和列表,以防列表中不存在提示值,而每次我们在其中写单词时都不返回false。一个列表 但如果值为True,则脚本将运行 如何运行此代码? 如果语法有误,我的语言是中等抱歉。
list = ["football", "tennis"]
answer = raw_input("What is your favorite sport?")
def fav():
for i in list:
if answer in list:
return answer
elif answer not in list:
while True:
answer2 = raw_input("There is not this sport in your profile please try again")
return answer2
print fav()
答案 0 :(得分:0)
不需要for
循环。另外,您的第一个raw_input
应该是该功能的一部分。
list = ["football", "tennis"]
def fav():
while True:
answer = raw_input("What is your favorite sport?")
if answer in list:
return answer
print "There is not this sport in your profile please try again"
print fav()
答案 1 :(得分:0)
list = ["football", "tennis"]
answer = raw_input("What is your favorite sport?")
def fav(answer):
while answer not in list:
answer = raw_input("There is not this sport in your profile please try again")
return answer
fav(answer)