def askme(answers):
question = raw_input("Do you want to see my name, age or favorite fruit?\n")
if not question in answers:
print "I have no answer for that, ask something else"
askme(answers)
answer_to_question = answers[question]
return answer_to_question
answers = {"name":"John", "age":18, "fruit":"apple"}
while True:
answer_to_question = askme(answers)
print "Answer is %s" % answer_to_question
我希望它如何运行:如果输入不在字典中,则要求新输入 如果输入在字典中,则使用输入作为键
出了什么问题:在我传入一个不在键中的值后,它要求我提供新的输入。如果我传入一个键中的值,我会得到一个 KeyError。
为什么会发生这种情况,我该如何解决?