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


Messages In This Thread
Beginner Boolean question [Guessing game] - by TKB - Mar-19-2022, 01:58 PM

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