Search code examples
pythonwhile-loopbreak

Why is the while loop unable to break?


answer = 5
guess = int(input('Please make a wild number guess: '))
count = 1

while guess != answer:
    count += 1
    int(input('wrong. Please make another guess: '))
    print(f"this is your {count} attempt") 

    if guess == answer:
        break
        print('Correct!!!')

i didn't get the answer i expected after i typed 5. i am still stucked in the while loop after typing the correct answer.

wrong. Please make another guess: 5 this is your 6 attempt


Solution

  • As @Soulfly mentioned, you need to update guess.

    Also, I suggest changing:

     if guess == answer:
            break
            print('Correct!!!')
    

    to

     if guess == answer:
            print('Correct!!!')
            break
    

    So the loop won't break before outputting the print statment.