Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,987 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.