max44 max44 - 1 month ago 6
Python Question

I have this function that is supposed to count points but it doesn't add them

I have this function that is supposed to count points but it doesn't add them:

def Correct(totalPoints):
print "Correct"
totalPoints = totalPoints+1
print "You have", totalPoints,"points"


Here is an example of what I use it in:

elif answer == 'nba':
NBAQues = random.randint(0,len(NBA1)-1)

NBAVar = NBA1[NBAQues]

print NBAVar
NBAAnswer = raw_input()
NBAAnswer = NBAAnswer.lower()
if NBAAnswer == NBA2[NBAQues]:
print Correct(totalPoints)
elif NBAAnswer != NBA2[NBAQues]:
print "False. The correct answer was", NBA2[NBAQues]
NBA1.remove(NBAVar)
NBA2.remove(NBA2[NBAQues])
print "Choose another."

Answer

I think you mix local and global namespaces. If you change your Correct(totalPoints) to this:

def Correct(totalPoints):
    return totalPoints + 1

and in your "main code":

print Correct(totalPoints)

to

totalPoints = Correct(totalPoints)
print "You have", totalPoints, "points"

it should work.

Hope this helps!