首页 > 解决方案 > Python 中的正确答案不止一个?

问题描述

我对编程很陌生,有点迷茫,所以如果我不能很好地解释自己,请原谅我。

例如,用户如何输入“y”或“yeah”并且仍然正确?

question = input("Does 1+1=2? ")

if question == "yes":
    print("Correct")
else:
    print("Incorrect")

编辑:

非常感谢大家的回答!阅读您的有用评论后,这对我有用:

question = input("Does 1+1=2? ")

accepted_answers = {"yes", "y", "yeah"}

if question in accepted_answers:
    print("Correct")
else:
    print("Incorrect")

标签: pythoninputboolean

解决方案


如果您想接受以 ay 或 Y 开头的任何内容,您可以使用

if question.lower().startswith("y"):

推荐阅读