Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Sravya Panganamamula
Sravya Panganamamula
1,990 Points

Number Game

Given below is my version of code for this problem. I am unable to understand what is wrong with it. It throws an error saying local variable 'guess' referenced before assignment. Please help. Given below is my code.

import random

def play_game():
    secret_num = random.randint(1,10)
    count = 1

    while count < 4:
        try:
            guess = int(input("enter a number between 1 and 10 "))
            print(guess)
        except ValueError:
            print("{} is not a number".format(guess))
        else:
            if guess == secret_num:
                print("you've hit it! My number was {}".format(secret_num))
                break
            elif (secret_num - guess) >= 5:
                count += 1
                if count == 4:
                    print("Game Over! My number was {}".format(secret_num))
                    choice = input("To continue the game please type PLAY else type QUIT ")
                    if choice == "PLAY":
                        play_game()
                    else:
                        print("Thank you")
                else:
                    print("""too low.
                    Remaining chances:{}""".format(4-count))
            elif (guess - secret_num) >= 5:
                count +=1
                if count == 4:
                    print("Game Over! My number was {}".format(secret_num))
                    choice = input("To continue the game please type PLAY else type QUIT ")
                    if choice == "PLAY":
                        play_game()
                    else:
                        print("Thank you")

                else:
                    print("""too high
                    Remaining Chances:{}""".format(4-count))
            else:
                count += 1
                if count == 4:
                    print("Game Over! My number was {}".format(secret_num))
                    choice = input("To continue the game please type PLAY else type QUIT ")
                    if choice == "PLAY":
                        play_game()
                    else:
                        print("Thank You!")
                else:
                    print(
                    """almosth there!
                    Remaining Chances: {}""".format(4-count))
play_game()

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The issues is in the try...except statement. If the guess can not be converted to an int then an error is raised before guess gets assigned. Use two statements to be sure guess gets a value:

        try:
            guess = input("enter a number between 1 and 10 ")
            guess = int(guess)
            print(guess)
        except ValueError:
            print("{} is not a number".format(guess))
        else:
            # ...
Sravya Panganamamula
Sravya Panganamamula
1,990 Points

Thank you for the solution. It was of great help.