How does the 'not' keyword work in this code? I'm trying to understand if 'not' reverses the value of 'out_of_guesses'
Question:
# Create a guessing game about your name, make the user have three tries only and end the game if the user cannot guess the answer in three tries.
answer = "jay"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess.lower() != answer and not out_of_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("You lose. ")
else:
print("You win. ")
I assigned a variable called out_of_guesses
in this case; however, the while statement should use the not
keyword and reverse the variable out_of_guesses
to True
. This isn’t the case however because if the not
keyword reversed the second condition to True
then it would not exit the loop when out_of_guesses
was True
. Basically, what I’m asking is how does the while
loop read the not
statement? Am I misunderstanding something and how?
Answers:
Perhaps you would better understand the logical expression if we inverted the sense of the boolean you have created:
...
have_more_guesses = True
while guess.lower() != answer and have_more_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
have_more_guesses = False
if have_more_guesses:
print("You win. ")
else:
print("You lose. ")
Note how I’ve inverted all the stages: the initialisation, the update and the final test.
This reads: "While you don’t have the right answer, but you have more guesses keep guessing."
# Create a guessing game about your name, make the user have three tries only and end the game if the user cannot guess the answer in three tries.
answer = "jay"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
while guess.lower() != answer and not out_of_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
out_of_guesses = True
if out_of_guesses:
print("You lose. ")
else:
print("You win. ")
I assigned a variable called out_of_guesses
in this case; however, the while statement should use the not
keyword and reverse the variable out_of_guesses
to True
. This isn’t the case however because if the not
keyword reversed the second condition to True
then it would not exit the loop when out_of_guesses
was True
. Basically, what I’m asking is how does the while
loop read the not
statement? Am I misunderstanding something and how?
Perhaps you would better understand the logical expression if we inverted the sense of the boolean you have created:
...
have_more_guesses = True
while guess.lower() != answer and have_more_guesses:
if guess_count < guess_limit:
guess = input("What's my name? ")
guess_count += 1
else:
have_more_guesses = False
if have_more_guesses:
print("You win. ")
else:
print("You lose. ")
Note how I’ve inverted all the stages: the initialisation, the update and the final test.
This reads: "While you don’t have the right answer, but you have more guesses keep guessing."