python - 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")
解决方案
如果您想接受以 ay 或 Y 开头的任何内容,您可以使用
if question.lower().startswith("y"):
推荐阅读
- javascript - 使用类型脚本根据数组中的某些项过滤两个数组
- csv - 在任何网站上以相同格式显示 .csv 内容
- c++ - 有没有办法将 8bitX32 ymm 寄存器右/左洗牌 N 个位置(c++)
- php - 无法分离和删除 morphToMany 关系
- apache-spark - 无法访问带有句点的名称的 PySpark 列。在名字里
- django - 如何保护 Django Rest API
- c++ - C++ #defines 在本地 .h 与 .h 中包含的对象
- php - 如何用超链接呼应作者?(在 WordPress 中)
- arrays - 仅使用指针算术访问整数指针数组的元素
- python - Graylog (graypy) + Docker