我已经用条件语句编写了一些代码,但我认为它不应该发生什么事情。
我试图多次重写代码。
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的输出为“您的输出为:淋浴”,但实际输出为“您的输出为:我已经准备好了。”
答案 0 :(得分:1)
不可能or
这样的条件,例如inputgetreadybrush == "Yes" or "yes" or "y" or "Y":
这将永远是正确的。它被解释为(inputgetreadybrush == "Yes") or "yes" or "y" or "Y":
如果答案不是“是”,则下一次测试or 'yes'
将被视为是。
最好写成:
inputgetreadybrush[0].lower() == 'y':
答案 1 :(得分:0)
为什么您要为一个简单的是/否答案写这么多字?
如果您仅尝试检查第一个字母,将会更容易。在这种情况下,您将看到答案的第一个字母是“ y ”还是“ n ”
例如,如果您具有 getready()函数,它将看起来更加清晰:
def getready():
inputgetreadybrush = input("Did you brush your teeth? ")
if inputgetreadybrush.lower()[:1] == "y":
inputgetreadyshower = input("Did you shower? ")
if inputgetreadyshower.lower()[:1] == "y":
print("Your output is: I already got ready. ")
else:
print("Your output is: Shower ")
elif inputgetreadybrush.lower()[:1] == "n":
inputgetreadyshower1 = input("Did you shower? ")
if inputgetreadyshower1.lower()[:1] == "y":
print("Your output is: Brush ")
else:
print("Your output is: Brush and Shower ")
# In case you want to truck if anithing else was press:
else:
print(f"What do you mean {inputgetreadybrush.lower()}? I do not understand...")
在这种情况下,人类将更容易更快地知道那里发生了什么。并且看起来会更游行:))
答案 2 :(得分:0)
将所有条件更改为正确的语法:
if (inputgetreadybrush == "Yes") or (inputgetreadybrush == "yes") or (inputgetreadybrush == "y") or (inputgetreadybrush == "Y"):
这解决了您所有的问题。