Home > Net >  Guess the word - Python (more guesses than should be)
Guess the word - Python (more guesses than should be)

Time:01-05

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")

CodePudding user response:

I see your query already answered above, In case you want I have adapted your code to make it more organized and structured into two functions

  1. playagain - which asks after winning/losing if the user wants to play the game again
  2. play - the actual program to play the game

I removed some redundancies like end = False variable you used and could have also removed some other variables like guess_limiter (by just assigning the guess limit value in the actual program) but I wanted to retain your code as much as possible in case for your understanding, hope this helps

    def playagain():
        play_again = input("Do you want to play again? (y/n) ")
        if play_again == 'y':
            return play()
        elif play_again == 'n':
            print("Thank you for playing")
        else:
            print("incorrect input please try again")
            playagain()
    
    def play():
        secret_word = "loading"
        guess_word = ""
        guess_count = 1
        guess_limit = 3
        print("Welcome to the guessing game\nYou have 3 guesses")
    
        while (guess_count != guess_limit   1):
            guess_word = input("Enter a word: ")
            if guess_word == secret_word:
                print("Correct, you win")
                playagain()
                break    
            elif guess_count == guess_limit:
                print("You have no more guesses, you lose!")
                playagain()
                break   
            else:
                print("Incorrect!")
                print("You have "   str(3 - guess_count)   " left!")
                guess_count  = 1
    
    play()

CodePudding user response:

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")
  • Related