python - Python代码没有做我认为应该做的事情
问题描述
我写了一些带有条件语句的代码,我认为它不应该做发生的事情。
我多次尝试重写代码。
def main():
def enter():
inputenter = input("Please enter a number. ")
if inputenter in ("1", "2", "3", "4", "5"):
getready()
else:
inputstartagain = input("Invalid Request")
def getready():
inputgetreadybrush = input("Did you brush your teeth? ")
if inputgetreadybrush == "Yes" or "yes" or "y" or "Y":
inputgetreadyshower = input("Did you shower? ")
if inputgetreadyshower == "Yes" or "yes" or "y" or "Y":
print("Your output is: I already got ready. ")
elif inputgetreadyshower == "No" or "no" or "N" or "n":
print("Your output is: Shower ")
else:
print("")
elif inputgetreadybrush == "No" or "no" or "n" or "N":
inputgetreadyshower1 = input("Did you shower? ")
if inputgetreadyshower1 == "Yes" or "yes" or "Y" or "y":
print("Your output is: Brush ")
elif inputgetreadyshower1 == "No" or "no" or "n" or "N":
print("Your output is: Brush and Shower ")
else:
print("")
main()
我希望(这些是 if 语句的答案)1,y,n 的输出是“你的输出是:淋浴”,但实际输出是“你的输出是:我已经准备好了。” 一切。
解决方案
它不可能or
像这样的条件inputgetreadybrush == "Yes" or "yes" or "y" or "Y":
这将永远是真的。它被解释为(inputgetreadybrush == "Yes") or "yes" or "y" or "Y":
如果答案不是“是”,则下一个测试or 'yes'
将被视为正确。
最好写成:
inputgetreadybrush[0].lower() == 'y':
推荐阅读
- uwp - 如何使用 ResoureDictionary 设置页面背景颜色?
- mongodb - MONGODB:检索与子文档相同的集合中的父文档
- ruby-on-rails - Rails zip数组按列匹配
- typescript - TypeGraphQL 为关系返回 NULL 值
- drake - TypeError: set_value(): 不兼容的函数参数
- javascript - Nuxt.js 插件未找到:/tmp/build_fb2cdbad/plugins/heap.js
- php - 无法捕获使用 $_POST 的 HTML 表单数据
- javascript - 如何完全停止 JavaScript setInterval 的执行?我已经尝试过 clearInterval,但它不起作用
- python - Python赌博骰子游戏?
- vbscript - 如何在 VBS 中监视以变量字符串开头的文件的文件夹