Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guessing Game does not work
#1
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?
Reply
#2
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!")
Reply
#3
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
Reply
#4
I posted problem to your calculator one here - https://python-forum.io/Thread-Python-Code-doesn-t-work
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Beginner Boolean question [Guessing game] TKB 4 2,226 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,847 Mar-20-2022, 08:16 PM
Last Post: menator01
  Guessing game problem IcodeUser8 7 3,520 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  Can not get quiz game to work funkymonkey123 3 2,518 Jul-02-2020, 03:56 PM
Last Post: chesschaser
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,683 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,130 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,218 Mar-09-2020, 04:53 PM
Last Post: snippsat
  guessing the number game go127a 6 4,775 Apr-27-2019, 01:23 PM
Last Post: go127a
  Guessing Game with .WAV Files - Python Coding NonEntity 8 4,295 Nov-20-2018, 12:53 AM
Last Post: NonEntity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020