Python Forum

Full Version: Guessing Game does not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i'm totally new to Python and I've been watching YouTube videos to get started with the language. In one of the videos, it showed us how to build a guessing game using while loops & if statements. I did the same, but it doesn't seem to work for me.

secret_word = "harry"
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 your guess: ")
        guess_count += 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("Out of guesses. Better luck next time!")
else:
    print("Congrats! You win!")
It gives me 3 tries, just like I specified. But when I type in the right word, it doesn't do anything. How do I fix this?
I see the problem. the loop will keep going and won't go to the if statement because your not out of guesses

secret_word = "harry"
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False
  
while guess != secret_word:
    if guess_count < guess_limit:
        guess = input("Enter your guess: ")
        guess_count += 1
        if guess != secret_word:
            print('try again')
    else:
        out_of_guesses = True
    if out_of_guesses == True:
        break
  
if out_of_guesses:
    print("Out of guesses. Better luck next time!")
else:
    print("Congrats! You win!")
I fixed the word guessing game. I dont know what the problem was, but I updated my 2018 PyCharm to 2019 PyCharm and it seemed to work. I have no idea why. Nevertheless, thanks for your help. But the calculator code still doesn't work. Help me with that, maybe? Thanks a lot man
I posted problem to your calculator one here - https://python-forum.io/Thread-Python-Code-doesn-t-work