Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the condition of my while loop 'apply' to my print() statement? [closed]

Only started learning Python a few days ago. I followed freecodecamp's while loop word guessing game tutorial and wanted to add the statement, "Wrong answer!" after every wrong guess.

This was what I initially wrote:

secret_word = "giraffe"
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 guess: ")
        guess_count += 1
        print("Wrong answer!")
    else:
        out_of_guesses = True

if out_of_guesses:
        print("Out of Guesses, YOU LOSE!")
else:
        print("Correct!")

While it did print after every wrong answer, it ALSO printed after the correct input, which was then followed by the "Correct!" statement.

Initially I tried moving print("Wrong answer!") to other lines within the while function and tried adding a break after the if function but nothing seemed to work.

It was only when I found this answer where "Wrong answer!" stopped printing after the correct input:

secret_word = "giraffe"
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 guess: ")
        guess_count += 1
        if guess == secret_word:
            print("Correct!")
        if guess != secret_word:
            print("Wrong answer!")
    else:
        out_of_guesses = True
        print("Out of Guesses, YOU LOSE!")

So my questions are:

  1. Why do I need to define if guess != secret_word: AGAIN if I already had while guess != secret_word and not (out_of_guesses): in the while loop function?

  2. Why did it keep printing "Wrong answer!" after the correct input in the first code even after adding a break?

like image 314
user29472044 Avatar asked Nov 15 '25 18:11

user29472044


2 Answers

The while loop (it's not a function!) head is (here) independent from what you do in the loop's body. When the loop condition is true, the whole code in the body runs. If that code prints something unconditionally (i.e., without using an if to check whether it should print), then you get that output. After the body is finished, the loop's condition is evaluated again, and the loop may or may not start over.

Stating it differently: the loop head determines if the body should run again, and that check happens when the body is finished. The whole body runs, unless you use break or continue to exit the loop early.

like image 138
Robert Avatar answered Nov 18 '25 06:11

Robert


Let's review your code (the second one):

Let's see the first line after the while (we will see the condition of it later)

while guess != secret_word and not (out_of_guesses):
    if guess_count < guess_limit:

Let's think about the first iteration, we haven't done anything, and we already ask if it has exceeded the number of attempts, it's true, it doesn't make sense, so we'll leave that line for later.

we then prompt the user to enter a word

guess = input("Enter guess: ")

then we compare it with the secret word, and if they match... we return True and exit the while, this avoids one of the while conditions.

if guess = secret_word:
    print("Correct!")
    break
else:
    print("Wrong answer!")
    

if it has not entered the if and exited the while, now if we increase the number of attempts

guess_count += 1

if the number of attempts has been exceeded, the appropriate message is printed and the while is exited.

if guess_limit == guess_count:
    print("Out of Guesses, YOU LOSE!")
      # the "out_of_guesses" variable becomes meaningless
    break

now we review the conditions of the while, since the comparison of the secret word and the entered one is done in the body of the while, we remove it from the conditions, the same happens with the limit of attempts, so we replace the previous conditions by:

while( True ): 
    guess = input("Enter guess: ")

    if guess = secret_word:
        print("Correct!")
        break
    else:
        print("Wrong answer!")

    guess_count += 1

    if guess_limit == guess_count:
        print("Out of Guesses, YOU LOSE!")
        break
like image 43
Marce Puente Avatar answered Nov 18 '25 06:11

Marce Puente



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!