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.