As @Cixate has mentioned, this can be fixed by setting correctAnswers = 0 inside getVerb and replacing the recursion with a while-loop:
def getVerb():
correctAnswers = 0
... the rest of your code, as before ...
... making sure to replace the recursion with a while-loop ...
The problem is that Python actually sees two different variables, in two different scopes, with the same name of correctAnswer. This causes something called "shadowing" to happen.
Because of this, Python will only use getVerb's version of correctAnswers when inside getVerb. And this variable was never given a value! So Python complains that you're trying to use it without having assigned it.
But ... wait! Didn't we assign a value? Well, we assigned to a variable named correctAnswers, but it was a different variable, with a different scope.
Check out the following examples:
x = 3
print "outer 1:", x # prints 3
def y1():
print "in y1:", x # print 3 -- !!
print "outer 2:", x # still prints 3
def y2():
x = 4
print "in y2:", x # prints 4 !!!
print "outer 3:", x # prints 3 ????
def y3():
print "in y3:", x # raises an exception!
x = 5
print "outer 4:", x # prints 3
y1()
y2()
print "outer 5:", x # still prints 3 !!!
try:
y3()
except:
print "y3 doesn't work!"
So, to sum up the craziness:
y1 sees the x from the outer scope -- declared on the first line
y2 creates its own x -- and can't see or use the other one
- it doesn't matter that
y2 assigned a new value to its own x, the outer x is still unchanged -- see outer 3 and outer 5
y3 also creates its own x -- but tries to use it before it has a value -> crash and burn!!
Making this one change will get your program working. Although it's not immediately necessary, you may eventually wish to improve your code quality, and learn how to write more idiomatic Python code. For example:
- replace the recursion with a while-loop
- use dictionaries
- use
raw_input instead of input
But this will come naturally with time and experience if you keep up the good work!