-1

I built a guessing game with some help. Why does the while loop terminate when only one condition is false if it's using and. Wouldn't or fit better here?

secret_word = "pirate"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False


while guess != secret_word and not(out_of_guesses):
    if guess_count < guess_limit:
        guess = input("Enter a guess:" )
        guess_count += 1
    else:
        out_of_guesses = True
        print("Out of guesses")

How does this work?

while guess != secret_word and not(out_of_guesses):
Na-s
  • 1
  • 2
  • 5
    Does `while not (guess == secret_word or out_of_guesses):` make more sense? This is an application of [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws). – chepner Nov 03 '20 at 01:46
  • 1
    Please post code as text, not as a screenshot. – Code-Apprentice Nov 03 '20 at 02:08
  • 1
    _How does this work?_ Which part are you stuck on? As an aside, please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/q/303812, https://meta.stackoverflow.com/q/285551, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Nov 03 '20 at 02:41

2 Answers2

1

The expression in while specifies when the loop should keep running. and means that both conditions have to be true for the expression to be true. So if either of the conditions is false, the and expression is false, and the loop stops.

If you change it to or, the expression is true if either condition is true. So you'll keep looping as long as the user doesn't guess the word, even if they've run out of guesses.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Got it! `and` returns true if both are true. `and` returns false if one is false. I thought the `and` meant both had to be false to return false. Can you help me understand `not()` more better? Thanks! – Na-s Nov 03 '20 at 02:20
  • `not` returns true if the operand is false, and returns false if the operand is true. – Barmar Nov 03 '20 at 02:21
  • if `not` returns true if it is false, then is the while condition is false since `out_of_guesses = False` and `not(out_of_guesses)` Wouldn't that make `out_of_guesses` true? – Na-s Nov 03 '20 at 02:52
  • It doesn't change `out_of_guesses`, it just returns the opposite of it. – Barmar Nov 03 '20 at 03:00
  • When `out_of_guesses` is False, `not out_of_guesses` is True. – Barmar Nov 03 '20 at 03:01
  • So wouldn't the program read `not(out_of_guesses)` as true and terminate it? – Na-s Nov 03 '20 at 03:18
  • 1
    No. The condition is for when to continue looping, not when to stop. – Barmar Nov 03 '20 at 03:19
  • If you said "while the sun is out I'll stay at the beach", you wouldn't leave the beach when the sun comes out, would you? – Barmar Nov 03 '20 at 03:21
  • I think I got it. `not` looks for a false value, and until it is changed it condition is true. Have to plant it in my head, the guy I'm learning from taught me in a weird way. – Na-s Nov 03 '20 at 03:31
  • 1
    Similarly: "while it's not raining I'll stay at the beach" – Barmar Nov 03 '20 at 03:33
  • @Na-s "not looks for a false value" I think this is the wrong way to think about it. `not`, `or` and `and` are operators that return a value. These operators act on boolean values as inputs. This is exaclty the same as how `-`, `+`, and `*` produce numbers as output when given numbers as input. The result of `not x` is to reverse the boolean value of `x`. So if `x` is `True`, then `not x` is `False` and vice versa. There are a lot of resources online that explain how this works, so don't rely only on your teacher. – Code-Apprentice Nov 03 '20 at 04:53
  • 1
    @Code-Apprentice I got it now. `True and True` returns `True`. `not` switches the boolean to true, which runs the loop. `or` wouldn't work because `True or False` just returns `True` and it would keep running. Thanks for your help, I'm glad I got it. – Na-s Nov 04 '20 at 23:26
  • @Na-s See also this question, which addresses a common error: https://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar Nov 04 '20 at 23:28
  • @Na-s Exactly! Glad you figured it out. – Code-Apprentice Nov 05 '20 at 01:17
0

We can use some variables to help describe the conditions:

guessed_wrong = guess != secret_word
has_more_guesses = not out_of_guesses
while guessed_wrong and has_more_guesses:
    # ...
    guessed_wrong = guess != secret_word
    has_more_guesses = not out_of_guesses

Now the wording should make it more clear why the loop should continue and why or is incorrect to use here.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268