-2

Why if i use OR at !!! section, does it break even if i type a number 1 - 9 in the guess input. And why do i not need to type BOTH 'q' and 'quit', because thats what i assume AND means... 'q' AND 'quit'...

import random

    while True:
        print("\nGuess a number between 1 and 9.")
        guess = input("I guess: ")

        if guess == 'q' !!!or!!! 'quit':
            break

        number = random.randrange(1, 10)
        try:
            if int(guess) < number:
                print(f"You guess was too low. The number was {number}")
            elif int(guess) > number:
                print(f"Your guess was too high. The number was {number}")
            elif int(guess) == number:
                print(f"Your guess was exactly right! The number was {number}")
        except ValueError:
            print("Please only guess numbers!")

So with OR it doesn't work and with AND it does. This makes no sense to me. Why is this?

Latika Agarwal
  • 973
  • 1
  • 6
  • 11
Sea Wolf
  • 81
  • 1
  • 6
  • @Carcigenicate I searched endlessly and didn't find anything i could understand, but i don't know why AND works still... – Sea Wolf Jun 07 '18 at 13:08
  • 2
    @SeaWolf show an example of using `and` in a similar case. – Carcigenicate Jun 07 '18 at 13:10
  • An equivalent example would be something like `a == b and c`. That *doesn't* work as youd expect though. – Carcigenicate Jun 07 '18 at 13:13
  • @Carcigenicate In this case exactly. If i swap !!!or!!! with !!!and!!! it works (without the !!! of course). Now i changed my code to be ... 'q' or guess == 'quit', as your answer suggested, but i still can't understand why AND in this instance as i have it also works. – Sea Wolf Jun 07 '18 at 13:13
  • @SeaWolf No, `and` doesn't do what you think it does in that case. It's exactly the same as `or` in that regard. I suspect you're falling for a false positive while testing. – Carcigenicate Jun 07 '18 at 13:14
  • @Carcigenicate Hmmm... yes you're right it doesn't work now. I don't know why it seemed to work. Thanks anyway. – Sea Wolf Jun 07 '18 at 13:17
  • 2
    Please don't interpret this as rude, because this is honest advice coming from experience: try not to make quick assumptions about the behavior of code based on insufficient testing. It's very easy to do a couple tests and draw conclusions about what is going on. I've spent hours trying to figure out why code is doing something unexpected, when really, I was misinterpreting results, and hadn't done enough testing to catch my mistake. – Carcigenicate Jun 07 '18 at 13:21

1 Answers1

1

if guess == 'q' or 'quit':

This statement will not work because you are trying to use a string as a boolean. The OR doesn't assume you want the exact same thing to happen like before it, you have to fill the condition again.

if guess == 'q' or guess == 'quit':

This will work because you are now getting a boolean out of the right side instead of just trying to use a string.

L_Church
  • 703
  • 1
  • 8
  • 19