Skip to content
Advertisement

Error: UnboundLocalError: local variable ‘attempts’ referenced before assignment

I am attempting to make a simple guessing game for my class with graphics and I’m trying to make an attempt counter. This is the area where my code is going wrong.

def value():

    guess = int(input("Enter your guess: "))
    if guess > num:
        attempts = attempts + 1
        turtle.clearscreen()
        interface()
        tooHigh()
        attempt = turtle.Turtle()
        attempt.speed(0)
        attempt.color("white")
        attempt.penup()
        attempt.hideturtle()
        attempt.goto(-250 , 200)
        attempt.write(guess, font=("Courier", 14, "bold"))
        value()
    elif guess < num:
        attempts = attempts + 1
        turtle.clearscreen()
        interface()
        tooLow()
        attempt = turtle.Turtle()
        attempt.speed(0)
        attempt.color("white")
        attempt.penup()
        attempt.hideturtle()
        attempt.goto(-250 , 200)
        attempt.write(guess, font=("Courier", 14, "bold"))
        value()
    elif guess == num:
        attempts = attempts + 1
        turtle.clearscreen()
        interface()
        yes()
        attempt = turtle.Turtle()
        attempt.speed(0)
        attempt.color("pink")
        attempt.penup()
        attempt.hideturtle()
        attempt.goto(-250 , 200)
        attempt.write(guess, font=("Courier", 14, "bold", "underline"))
        print ("Correct!")
    else:
        print ("ERROR")

def startScreen():

    begin = input("Start the game?: ")
    if begin == 'yes':
        value()
    elif begin == 'instructions':
        instructions()
        startScreen()
    elif begin == 'no':
        sys.exit()
    else:
        print ("Unrecognised answer.")
        startScreen()

attempts = 0    
num = random.randint(1,1000)  
interface()   
startScreen()

The error I receive is:

Traceback (most recent call last):
  File "D:DesktopPython ProgramsGame.py", line 154, in <module>        
    `startScreen()`        
  File "D:DesktopPython ProgramsGame.py", line 141, in startScreen        
    `value()`        
  File "D:DesktopPython ProgramsGame.py", line 110, in value        
    `attempts = attempts + 1`        
UnboundLocalError: local variable 'attempts' referenced before assignment      

It doesn’t seem possible to move attempts into the function as it constantly calls itself, resetting attempts each time.

I am unsure why this is occurring so any help would be greatly appreciated. Thanks!

Advertisement

Answer

The reason you are getting this error is because of something called variable scope. You have defined the variable attempts outside of the function value, so the function will not recognize any local variable by the name attempts unless you explicitly put the statement global attempts at the beginning of your function. This will tell the function that you want to use the variable attempts that you defined in your main program. That should look like this:

def value():
       global attempts
       #function code

Alternatively, you can allow your function value to take an argument, and pass the variable attempts into that. That would look something like this:

def value(attempts):
       #function code
       #you can use the variable attempts as needed, since you're passing it directly into the function

#call the function however you want
attempts = 0
value(attempts)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement