0

I have written some code with conditional statements and I don't think it's supposed to do what happens.

I tried to rewrite the code many times.

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()

I expected the output of (these are the answers to the if statements) 1,y,n to be "Your output is: Shower" but the actual output is "Your output is: I already got ready. " for everything.

  • 1
    [How to write a good title](https://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title) – csabinho Aug 06 '19 at 23:04
  • 1
    See [truth value of a string in Python](https://stackoverflow.com/questions/18491777/truth-value-of-a-string-in-python). When you do `or "yes"`, that's essentially putting `or True` in your conditional, causing it to execute. – Stephen B Aug 06 '19 at 23:17

3 Answers3

1

Its not possible to or conditions like inputgetreadybrush == "Yes" or "yes" or "y" or "Y":

It will always be true. It is interpreted as (inputgetreadybrush == "Yes") or "yes" or "y" or "Y":

And if the answer isn't Yes, the next test, or 'yes' will be counted as true.

It could be better written as:

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

Chris Charley
  • 6,403
  • 2
  • 24
  • 26
0

Why you try to write so many words to a simple yes/no answer?

Will be more easy if you will try to check just the first letter. In this case you will see if first letter of the answer is "y" or "n"

For the example your getready() function will look more clear if you will have this:

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...")

In this case will be more easy for humans to know what is happen there much faster. And will look more processional :))

0

Change all of your conditions to the correct syntax:

if (inputgetreadybrush == "Yes") or (inputgetreadybrush == "yes") or (inputgetreadybrush == "y") or (inputgetreadybrush == "Y"):

This solves all of your problems.

Zakariah Siyaji
  • 989
  • 8
  • 27