McCS17042
McCS17042

Reputation: 1

what is causing reference before assignment errors in below code?

I'm getting this error with refrenced before assignment and im not sure how to fix it.

I havent tried anything at the moment. It would be appreciated if this could be answered. (im just trying to fill up more words so it can be posted)

this is the error code i am getting:

number = int(number)

UnboundLocalError: local variable 'number' referenced before assignment

And this is the rest of my code

import random
import sys

again = True









while True:
    myName = input('Hello, Enter your name to get started')
    if myName.isdigit():
        print('ERROR,Your Name is not a number, please try again')
        print('')
        continue
    break
myName = str(myName.capitalize()) 




print('')    
print('Hi {}, This is Guessing Game, a game where you have a certain amount of attempts to guess a randomly generated number. Each level has a different amount of attempts and a higher range of number. After each guess, press enter and the program will determine if your guess is correct or incorrect.' .format (myName))

print('--------------------------------------------------------------------------------')





while True:
    level = input('{}, Please select a level between 1 and 3. Level 1 being the easiest and 3 being the hardest')
    if not level.isdigit():
        print('Please enter a number between 1 and 3. Do not enter a number in word form')
        continue
    break

def guessNumber(): # Tells the program where to restart if the user wants to play again
    guessesTaken = 0
    List = []



    if level == 1:
        number = random.randint (1, 16)
        print('You chose Level 1, Guess a number a between 1 and 16, you have 6 guesses.') 
        allowedGuesses = 6 
        boundary = 16 

    if level == 2: # The code for level 2
        number = random.randint (1,32)
        print('You chose Level 2, Guess a number between 1 and 32, You have 8 guesses.')
        allowedGuesses = 8
        boundary = 32


    if level == 3:
        number = random.randint (1, 40)
        print('You chose Level 3, Guess a number between 1 and 40, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 40

    if level == 4:
        number = random.randint (1, 50)
        print('You chose Level 4, Guess a number between 1 and 50, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 50

    if level == 5:
        number = random.randint (1, 60)
        print('You chose Level 5, Guess a number between 1 and 60, you have 10 guesses.')
        allowedGuesses = 10
        boundary = 60





        guess = input() 
        guess = int(guess)
        while guessesTaken < allowedGuesses:



            guessesTaken = guessesTaken + 1 
            guessesLeft = allowedGuesses - guessesTaken





        if guess < number:  
            List.append(guess)
            print('Your guess is too low, You must guess a higher number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))


        if guess > number: 
            List.append(guess)
            print('Your guess is too high, You must guess a lower number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))


        if guess > boundary: 
            List.append(guess)
            print('You must input a number between 1 and 16. You have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))

        if guess == number:
            List.append(guess)
            print('Good Job {}!, You guessed my number in {} guesses. You guessed the numbers {}.' .format (myName, guessesTaken, List))
            print('Your high score for your previous game was {}' .format(guessesTaken))





    else:
        number = int(number)
        print('')
        print('--------------------------------------------------------------------------------')      
        print('Sorry {}, Your gueses were incorrect, The number I was thinking of was {}. You guessed the numbers {}.' .format(myName, number, List))







guessNumber()
print('')
print('It is recommended to pick a harder level if you chose to progress')
print('')

while True:
    again = input('If you want to play again press 1, if you want to stop playing press 2')
    if not again.isdigit():
        print('ERROR: Please enter a number that is 1 or 2. Do not enter the number in word form')
        continue
    break





if again == 1:
    level + 1
    guessNumber()






if again == 2:
    print('Thanks for playing Guessing Game :)')
    sys.exit(0)

Upvotes: 0

Views: 55

Answers (2)

Barmar
Barmar

Reputation: 781068

Since level is a string rather than a number, none of the conditions like

if level == 1:

will succeed. So none of the assignments like number = random.randint (1, 16) ever execute, and number is never assigned.

Since if level == 5: doesn't succeed, it goes into the else: block, which starts with

number = int(number)

Since none of the other number assignments took place, this tries to use int(number) before the variable has been assigned, which doesn't work.

I'm not sure why you even have this assignment there. When number is assigned, it's always set to an integer, so there's no need to use int(number).

You need to use

level = int(level)`

after you confirm that it contains digits. And you need to do similarly with again.

There are a number of other problems with your code. For instance, the code that asks for the user's guess and checks it is inside the if level == 5: block, it should run in all the levels.

When you have a series of mutually exclusive tests, you should use elif for each successive test. If you just use if for each of them, and then use else: at the end, that else: will only apply to the last test, so it will be executed even if one of the early tests also succeeded.

Upvotes: 0

Naveen Jain
Naveen Jain

Reputation: 1072

In your code you are getting level as input and checking that if level is in between 1 to 5.
else you are trying number = int(number)
but you should write number = int(level).

Upvotes: 1

Related Questions