Please help debug the code - beginner

Hi

I’ve recently started learning python by watching Youtube videos. One example I came across is to code a guessing time, with the answer being “Monkey” and one can take 3 guesses max.

My code is as follows:

However, for the 3rd guess, despite the guess being wrong, it printed out “You win” (see below). Could someone please help me debug this and opine on how the code should be revised to achieve the desired result? Many thanks!

Enter guess: da
Enter guess: d
Enter guess: dafs
You win!

Please post your actual code instead of a picture. Thanks

Hi!

There are a tutorial of freecodeCamp where use the same example at 2:20:00

Welcome to the forum @wabfff

The issue is the if statement.

If the guess count is less than the guess limit, print "You win!"
You could change it to if guess is equivalent to secret_word.

Happy coding

Hey there, i think the current problem with the code “you win” is being printed regardless of whether the user’s final guess is correct / not, you need to make ensure “you win” is printed if guess matches secret _word, sorry i used (monday)

Thank you for mentioning this. I was literally following that video lol, but had a different thought in terms of codes and then landed with the above.

Managed to fix this, Thank you @Teller @06Douglas for your insights!

New code as follows:

secret_word = "Monkey"
guess = ""
guess_count = 0
guess_limit = 3

while guess != secret_word and guess_count < guess_limit:
        guess = input("Enter guess: ")
        guess_count += 1

if  guess == secret_word:
    print("You win!")
else:
    print("You lose!"
1 Like

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Thank you . Will make a note of this for future posts :slight_smile:

1 Like

Roger that, i will try to perfect next time…