Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Guess the word – Python (more guesses than should be)

I am trying to make a game where the user inputs a word with only three guesses available, but instead it keeps allowing four guesses which I don’t want.
When i input a word and my guess_count reaches 3, i am supposed to be sent this msg "You have no more guesses, you lose!" then ask if i want to retry but instead it lets me play one more time thereby exceeding the guess_limit

Here is my code

  secret_word = "loading"
  guess_word = ""
  guess_count = 0


  guess_limit = 3

  end = False

  print("Welcome to the guessing game\nYou have 3 guesses")
  guess_word = input("Enter a word: ")

  def end_msg(msg):
      print(msg)
      retry = input("Do you want to play again? ")  
      if (retry == "yes") :
          global end, guess_word, guess_count
          end = False
          guess_word = ""
          guess_count = 0
          print("Welcome to the guessing game\\nYou have 3 guesses")
          guess_word = input("Enter a word: ")
      else:
          end = True

  while (not(end)) :
      if (guess_word != secret_word and guess_count \< guess_limit):
          guess_count += 1
          print("Incorrect!")
          print("You have " + str(3 - guess_count) + " left!")
          guess_word = input("Try again: ")
      elif (guess_count == 3):
          end_msg("You have no more guesses, you lose!")
      else:
          end_msg("Correct, you win")

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are getting this bug because you wrote guess_limit = 3. You know Programs count from 0, so you need to enter 1 less from your desire try. It should be guess_limit = 2.
And also you wrote elif (guess_count == 3) :, thats mean if guess_count == 3 your will get "You have no more guesses, you lose!" even, user put the right answer. so it should be

elif (guess_word != secret_word and guess_count == 2):

so your final code should be:

secret_word = "loading"
guess_word = ""
guess_count = 0


guess_limit = 2

end = False

print("Welcome to the guessing game\nYou have 3 guesses")
guess_word = input("Enter a word: ")

def end_msg (msg) :
    print(msg)
    retry = input("Do you want to play again? ")
    if (retry == "yes") :
        global end, guess_word, guess_count
        end = False
        guess_word = ""
        guess_count = 0
        print("Welcome to the guessing game\\nYou have 3 guesses")
        guess_word = input("Enter a word: ")
    else:
        end = True

while (not(end)) :
    if (guess_word != secret_word and guess_count < guess_limit) :
        guess_count += 1
        print("Incorrect!")
        print("You have " + str(3 - guess_count) + " left!")
        guess_word = input("Try again: ")
    elif (guess_word != secret_word and guess_count == 2) :
        end_msg("You have no more guesses, you lose!")
    else:
        end_msg("Correct, you win")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading