Python Forum
Beginner Boolean question [Guessing game]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner Boolean question [Guessing game]
#1
Question 
Dear all,

I recently decided to study Python (no prior experience). I took the beginner course available on Youtube: https://www.youtube.com/watch?v=rfscVS0v...deCamp.org.

My question concerns the part where we are instructed to build a guessing game (starts at 2:20:00 in the video). The final code reads as follows:

secret_word = "cat"
guess = ""
guess_total = 0
guess_limit = 3
out_of_guesses = False

while guess != secret_word and not(out_of_guesses):
    if guess_total < guess_limit:
        guess = input("Enter guess: ")
        guess_total += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("you lose, out of guesses!")
else:
    print("you win!")
The above code works just fine. However, what I can't truly understand the boolean logic in this code. Here is my logic:

1. We first define "out_of_guesses" as "False". My understanding is that this is done to establish a boolean value to be used later when breaking out of the 'guessing' loop. If this is not defined, we cannot give the answer "you lose, out of guesses" later. This boolean value might as well be "True", on condition that the other values are reversed as well - the code would still work as intended.

2. In the while loop text the "out_of_guesses" is negated by a 'not' operator. To my understanding not(out_of_guesses) is the same as out_of_guesses = True (because out_of_guesses was defined initially as False and not(False) is True). This "not(out_of_guesses)" text is inserted in the while loop to have two points of reference, i.e. the first one is "guess != secret_word", which means as long as the guessed word is not the secret word, we keep on guessing, and second, while we are not out of guesses, we keep guessing as well.

3. However, going further down the code we see that it says "else: out_of_guesses = True", meaning that if we actually run out of guesses (3 guesses allowed), we establish this boolean as True. But isn't this boolean already "True" in the above line "not(out_of_guesses) ? I.e. in my understanding nothing has changed as the code in my opinion reads as folows: While we have not guessed the correct word and while X is True (or not(False) -> ask for input. However, While the correct word is not answered and we have run out of guesses, set the X as True.

4. Shouldn't the code say "While guess != secret_word and out_of_guesses: [..] else: out_if_guesses: True"?

What am I missing here? I really can't move forward without understanding where is my logic flawed Cry

Any input is much appreciated! Thank you!

p.s. I have also attached photo of the code.

-TKB
Yoriz write Mar-19-2022, 02:19 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

Thumbnail(s)
   
Reply
#2
outofguesses = False
not outofguesses = True
so continue.

outofguesses = True
not outofguesses is False
exit while loop
Reply
#3

  1. out_of_guesses has initially been assigned as False because the following while statement will only loop if this is False
  2. not isn't a function its a logical operator the brackets are not needed here, not will evaluate to True if the statement(s) are not True, otherwise, it will evaluate to False
  3. out_of_guesses isn't already True because not doesn't change the asignment of out_of_guesses
  4. The while loop would not loop at all in this case, it would only loop if out_of_guesses was assigned to True
Reply
#4
The key here is the while loop, which tests two conditions. If either the guess is correct OR you are out of guesses the while loop exits. Then you need to see why you exited. Easiest way to tell if you won or lost is by looking at out_of_guesses. If you are out of guesses, you lost. If you are not, then the only way you exited the loop is that you won.

So a bit of confusion about the not operator. The script uses out_of_guesses as a boolean, and it is appropriately named. This also means that it needs to start as False and be changed to True once the number of guesses is beyond the number of allowed guesses.

OK, but a better approach does not use out_of_guesses, because that is a derived value from the guess_total and guess_limit, uses a for loop to limit the number of guesses, does not give an artificial first value to guess, would be IMHO
secret_word = "cat"

guess_total = 0 #we don't use out_of_guesses as that is derived from guess_total and guess_limit
guess_limit = 3
 
for guess_total in range(guess_limit) : #use for loop to limit the number of guesses
    guess = input("Enter guess: ")
    if guess == secret_word :
        break #breaks out of the for loop if match
 
if guess != secret_word :
    print("you lose, out of guesses!")
else:
    print("you win!")
Reply
#5
This is not Python code. It is Basic or C or some other language translated poorly to Python. I would write the code to take advantage of the Python for/else loop. Please excuse the excessive commenting.
secret_word = "cat"  # Fewer variables
guess_limit = 3

for _ in range(guess_limit):  # Does the guess counting.  Not using loop counter, so replace with "_".
    if input("Enter guess: ") == secret_word:  # No need for "guess" variable
        print("You won!")
        break  # Exit out of loop
else:  # Following block only runs if for loop runs to completion
    print("you lose, out of guesses!")  # 8 fewer lines with no loss in legibility
This code uses for/else to manage the guesses. If you guess correctly the program prints the win message and breaks out of the for loop. If you keep guessing wrong the for loop will execute 3 times and exit because it is complete. The program uses the for/else condition to run some code if the loop completes. Here I print the lose message because to execute the "else" code the for loop executed 3 times without the user entering a correct answer.

Back to your original question.
Quote:2. In the while loop text the "out_of_guesses" is negated by a 'not' operator. To my understanding not(out_of_guesses) is the same as out_of_guesses = True (because out_of_guesses was defined initially as False and not(False) is True). This "not(out_of_guesses)" text is inserted in the while loop to have two points of reference, i.e. the first one is "guess != secret_word", which means as long as the guessed word is not the secret word, we keep on guessing, and second, while we are not out of guesses, we keep guessing as well.
"not out_of_guesses" (there is no need for parenthesis) is not the same as out_of_guesses = True. It is more accurate to say it is the same as out_of_guesses == False, but that isn't correct either. out_of_guesses will evaluate to bool True or False based on it's value. If True, not out_of_guesses == False, and if False, not_out_of_guesses == True.

Python allows values other than True and False to be used in boolean expressions. C does this too, with 0 == False and 1 == True. But Python has more things that can be used in boolean expressions. In fact, any expression in Python can be used in a boolean expression. The code below prints the True-ness or False-ness of some expressions.
for out_of_guesses in (0, 1, "", "hi", [], [1], None, True, False):
    print(out_of_guesses, bool(out_of_guesses), not out_of_guesses, out_of_guesses == False)
Output:
0 False True True 1 True False False False True False hi True False False [] False True False [1] True False False None False True False True True False False False False True True
As with C, 0 evaluates to bool False, but so do "" (empty string), [] (empty list), None and False. As with C, 1 evaluates to bool True, but so do "1" (non-empty string), [1] (non-empty list) and True and almost everything else.

The important thing to note, and the reason I write this reply, is that the last two values in each line are not always the same. These are the values for "not out_of_guesses" and "out_of_guesses == False". You would think that for "not out_of_guesses" to be True, "out_of_guesses" must be False, and this is correct. But "must be False" is not the same as "== False". For empty strings, lists, tuples... and for None, not x is different than x == False. not "" (empty string) is True, because an empty string is bool False, but "" does not equal False because an empty string is an empty string, and not a boolean value.

This is a good reason why you should never write code like this:
if x == True:
Not only is the "== True" redundant, it can generate unexpected results.

Assume x == 5
bool(x) is True because bool(5) is True
5 != True because 5 is an int and True is a boolean.
"if x:" executes the following code. "if x == True" does not.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Very Beginner question on simple variables Harvy 1 169 Apr-12-2024, 12:03 AM
Last Post: deanhystad
  Beginner Higher Lower Game wallytan 2 1,584 Sep-29-2022, 05:14 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,700 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Unable to count the number of tries in guessing game. Frankduc 7 1,901 Mar-20-2022, 08:16 PM
Last Post: menator01
  Beginner question NameError amazing_python 6 2,444 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  Beginner question - storing values cybertron2 4 3,199 Mar-09-2021, 04:21 AM
Last Post: deanhystad
  question about bot for mmo game rachidel07 1 2,031 Jan-21-2021, 12:45 PM
Last Post: Aspire2Inspire
  beginner question about lists and functions sudonym3 5 2,725 Oct-17-2020, 12:31 AM
Last Post: perfringo
  Tic-Tac game (Beginner's coding) Shahmadhur13 5 3,100 Aug-29-2020, 08:40 PM
Last Post: deanhystad
  Guessing game problem IcodeUser8 7 3,618 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020