如何在每次检查时提示,如果为假则返回假,如果为真则显示结果?

时间:2018-07-05 23:44:53

标签: python python-2.7

我的问题是:假设我们在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()  

2 个答案:

答案 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)