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.
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
globalstatement to tell Python you want to assign to a global, not a local value, meaning that if you doglobal guessesbefore 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:
Firstly we use a
whileloop 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 toguessesrather than using globals.