首页 > 解决方案 > 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 的输出是“你的输出是:淋浴”,但实际输出是“你的输出是:我已经准备好了。” 一切。

标签: pythonrepl.it

解决方案


它不可能or像这样的条件inputgetreadybrush == "Yes" or "yes" or "y" or "Y":

这将永远是真的。它被解释为(inputgetreadybrush == "Yes") or "yes" or "y" or "Y":

如果答案不是“是”,则下一个测试or 'yes'将被视为正确。

最好写成:

inputgetreadybrush[0].lower() == 'y':


推荐阅读