
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:
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
Any input is much appreciated! Thank you!
p.s. I have also attached photo of the code.
-TKB
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

Any input is much appreciated! Thank you!
p.s. I have also attached photo of the code.
-TKB