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 Python Basics (2015) Number Game App String length

Sam Bui
Sam Bui
878 Points

UnboundLocalError in number_game exercise (Python Basic)

Hi, after followed the instructor to build the game. I try to improve it. Everything works just fine until I input a letter in, then it say:

File "number_game2.py", line 26, in get guess return player_number UnboundLocalError: local variable 'player_number' referenced before assignment

I don't understand what it means and why I got it, I am very appreciated if you can explain it to me and how to fix it.

Thank you

strlen.py
import random
import sys
import os

def clear():
    if os.name == 'nt':
        os.system('cls')
    else:
        os.system('clear')

# create a function for guessing and condition for legit input
def get_guess(bad_guesses):
    clear()
    while True:
        try:
            guess = input('guess a number from 0 to 9: ')
            player_number = int(guess)
        except ValueError:
            print('you can only guess number.')
        else:
            # player guess more than 1 character:
            if len(guess) != 1:
                print('you can only guess one number!')
            # player guess a number second time
            elif player_number in bad_guesses:
                print('you already guess this number.')
        return player_number

# create function that tell player how many tries they have left & print out their bad guesses
def indicator(bad_guesses):
    print('Strike {}/5.'.format(len(bad_guesses)))
    for number in bad_guesses:
        print(number)
    print('')

# function for actual game
def number_game(done):
    clear()
    # let the machine pick a random number
    secret_number = random.randint(0, 9)
    # create a list for bad guesses
    bad_guesses = []
    # let ask the player
    while True:
        indicator(bad_guesses)
        guess = get_guess(bad_guesses)

        # if guess is legit, then consider whether the guess is correct or not
        # if the guess is correct
        if guess == secret_number:
            print('you win!')
            print('{} is the secret number.'.format(secret_number))
            done = True

        # if the guess is wrong
        else:
            bad_guesses.append(guess)
            # if guess is lower than secret number
            if guess < secret_number:
                print('the secret number is greater than {}.'.format(guess))
            # if guess is greater than secret number
            elif guess > secret_number:
                print('the secret number is lower than {}.'.format(guess))
            # if player fail to guess the right number in 5 turns
            if len(bad_guesses) == 5:
                print('you lose!')
                print('the secret number is {}.'.format(secret_number))
                done = True

        if done:
            while True:
                play_again = input('play again? Y/n ').lower()
                if play_again == 'y':
                    return number_game(done=False)
                elif play_again == 'n':
                    sys.exit()
                elif play_again != 'y' and play_again != 'n':
                    continue


def welcome():
    print('Hello, welcome to number game.')
    start = input("press enter/return to play or 'Q' to quit: ").lower()
    if start != 'q':
        number_game(done)
    else:
        print('Bye Bye!')
        sys.exit()

done = False
welcome()

:)

3 Answers

Hey Sam, I found the clear() worked pretty well here:

while True:
        try:
            guess = input('guess a number from 0 to 9: ')
            clear()

it clears the screen and shows all the information. Nice work on your code.

Hi Sam. I did not get that error. However it looked to me like the placement of clear() might be preventing some things from displaying that you want to be seen.

Sam Bui
Sam Bui
878 Points

Hi Join, oh yeah, that's true too, the clear() in get_guess function. I want to show player if their number is too high or too low when they guess wrong, but the messages never show up. I know the clear() function causes it. I tried several ways to fix it but no luck. Any idea how to fix it?

Sam Bui
Sam Bui
878 Points

Hi John, it works fine now, thank you for your help :)