How to create guess counter in guessing game

I'm trying to make a number guessing game where when you guess the number right, it tells you how many guesses it took.

I've tried several loops but can't figure out how to get my "guesses" to increase.

import random

rand_num = random.randrange(1,201)


def guess_game():

    guess = int(input("Please enter your guess: "))
    guesses = 1
    if guess == rand_num:
        print("Hit!\nIt took you " + str(guesses) + " guesses!")
    elif guess < rand_num:
        print("Your guess is too low.")
        guesses = guesses + 1
        guess_game()
    else:
        print("Your guess is too high")
        guesses = guesses + 1
        guess_game()

guess_game()

For example, desired output should be something like this:

"Hit! It took you 5 guesses"

But it only says 1 guesses no matter how many tries it took.

3 answers

  • answered 2019-09-10 02:21 Grismar

    Your code doesn't work because you keep calling guess_game() after every guess, effectively starting a new game after every guess.

    When the user finally guesses correctly, it's always after 1 guess in that new game and then all games end at once, but never does the code reach the line where it prints the number after more than one guess in any of those games.

    There's many different ways to fix this, but the main issue here is that (like many new programmers) you didn't realise calling a function doesn't just get the program to jump to your new code, it creates an entirely new space for the program to work in (each call to the function gets its own 'scope') and returns from that once the function is done, with the previous scope unchanged in most cases.

  • answered 2019-09-10 02:43 Matthew Gaiser

    Every time you call guess_game(), you are essentially resetting the game.

    Instead, you want to enclose your game in a while loop which it only exits when the game is over. I have written a working version for you here:

    import random
    rand_num = random.randrange(1,201)
    def guess_game():
        guesses = 1
        while(True):
            guess = int(input("Please enter your guess: "))
            if guess == rand_num:
                print("Hit!\nIt took you " + str(guesses) + " guesses!")
                return 0
            elif guess < rand_num:
                print("Your guess is too low.")
                guesses = guesses + 1
            else:
                print("Your guess is too high")
                guesses = guesses + 1
    
    guess_game()
    

  • answered 2019-09-10 16:44 Rizwan Amjad

    Your Code is not working well because you have initialize guesses=1 in function when you call the function in itself (Recursion) the value of variable guesses is reset.

    import random    
    rand_num = random.randrange(1,201)
    
    guesses=0 #initialize it outside function
    
    
    def guess_game():
        guess = int(input("Please enter your guess: "))
        global guesses 
        guesses+=1
        if guess == rand_num:
            print("Hit!\nIt took you " + str(guesses) + " guesses!")
        elif guess < rand_num:
            print("Your guess is too low.")
            guess_game()
        else:
            print("Your guess is too high")
            guess_game()
    
    guess_game()