我是Python的新手,我试图创建一个测验,但我不知道如何制作这个程序。
测验是一个有差距的短语,你需要在空白中加入正确的词。如果单词正确,则在空白处打印带有正确单词的短语。如果错误,请再次打印。
我正在尝试这样的事情:
phrase = 'HTML is HyperText __1__ __2__.'
answers = ['Markup','Language']
def quiz(p,a):
""" I can do it manually with if statements,
but this is not a good idea.""""
有人可以帮助我吗?
答案 0 :(得分:0)
此方法按顺序迭代答案(根据您的评论)。
要遍历每个答案,使用for
循环。在for
循环内部是一个while
循环,它会不断地询问用户输入,直到他们得到正确的答案,然后更新短语并突破while
循环。 / p>
blank = "_____"
phrase = ["HTML is HyperText ", blank, " ", blank]
answers = ['Markup','Language']
def quiz(phrase, answers):
for answer in answers:
while True:
# "".join() converts the list into a string
inp = raw_input("".join(phrase)+" ") #replace with input() in python3
if inp == answer:
# Updates the first available blank with the answer
phrase[phrase.index(blank)] = answer
break
考虑添加.lower()
函数以使输入不区分大小写。在raw_input上调用它并回答。
if inp.lower() == answer.lower():