-1

Possible Duplicate:
referenced before assignment error in python


So I've started to try to teach myself a little Python, and I've come up against my first error already.
When I try to increment 'guesses' (I want to be able to display how many guesses it's taken) in the else statement I get a reference before assignment error, which I don't understand, as I've assigned 'guesses' a value before the start of the function.

Here's my code -

import random

def guessFunc():
    guess = input("Guess a number between 1 and 10: \n")
    guess = int(guess)

    if guess == num:
        print("Congratulations, you got it right")

    else:
        guesses += 1 
        guessFunc()


num = random.randint(1,10)
guesses = 1
guessFunc()

What confuses me more is that fact that if I put

print(guesses)

into the start of my function it will print the value I've assigned 'guesses'. I really just don't get how the function can see the value and can print it, but can't change it.

If anyone could expalin to me why this happens, I'd be really grateful, I 'm guessing it's a local/global thing but I'm really not sure.

Community
  • 1
  • 1
matt
  • 3
  • 1

2 Answers2

2

Python scoping allows values from outer scopes to fall into scope for reading. So you can access and use a global from a function as you describe with the print() statement.

However, when you want to assign, you need to use the global statement to tell Python you want to assign to a global, not a local value, meaning that if you do global guesses before you try to assign to it, it will work. As you don't here, it will see the assignment and therefore presume you want to create a local variable, but as you are doing += it needs the existing value of the new local variable, which of course doesn't exist, causing an error.

Do note that globals are considered bad practice in general and are best avoided as they make it very hard to see what a function is going to do. It is far better to make the function return a value and work with that.

A better way to do what you want would be something like this:

import random

def guessFunc():
    guesses = 0
    while guess != num:
        guess = input("Guess a number between 1 and 10: \n")
        guess = int(guess)
        guesses += 1 
    print("Congratulations, you got it right")
    return guesses

num = random.randint(1,10)
guesses = guessFunc()

Firstly we use a while loop to repeat the action, as recursion is not optimized in Python, then we return the number of guesses performed from the function, and assign it to guesses rather than using globals.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • Thanks so much, that's really cleared things up for me. Appreciate you taking the time out to answer what now seems a pretty stupid question. – matt Dec 10 '12 at 19:53
0

The use of guesses in the function refers to a local variable of that name. And since you have not assigned such a local variable, the program fails when you attempt to read from it.

You need to tell the interpretor that you want to refer to the global variable.

def guessFunc():
    global guesses
    guess = input("Guess a number between 1 and 10: \n")
    guess = int(guess)

    if guess == num:
        print("Congratulations, you got it right")

    else:
        guesses += 1 
        guessFunc()
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490