I have this function that is supposed to count points but it doesn\'t add them:
def Correct(totalPoints):
print \"Correct\"
totalPoints = totalPoints+1
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!
totalPoints
is being cloned inside your function. To do what you want, you should set totalPoints
based on the function result, like so:
def Correct(totalPoints):
print "Correct"
totalPoints = totalPoints+1
print "You have", totalPoints,"points"
return totalPoints
totalPoints = Correct(totalPoints)
This ignores why you would want such a function in the first place, but it should work.
You are not returning the function output. It should be
def Correct(mytotalPoints):
print "Correct"
mytotalPoints += 1
print "You have", mytotalPoints,"points"
return mytotalPoints
You should now return the correct value to the main function. You should also fix the indentation to be correct. You need to show where totalPoints was defined.
elif answer == 'nba':
NBAQues = random.randint(0,len(NBA1)-1)
# Is this the way you meant it to be indented?
NBAVar = NBA1[NBAQues]
print NBAVar
NBAAnswer = raw_input()
NBAAnswer = NBAAnswer.lower()
if NBAAnswer == NBA2[NBAQues]:
# Make sure totalPoints was initialized properly
totalPoints = Correct(totalPoints)
print totalPoints
elif NBAAnswer != NBA2[NBAQues]:
print "False. The correct answer was", NBA2[NBAQues]
NBA1.remove(NBAVar)
NBA2.remove(NBA2[NBAQues])
print "Choose another."
totalPoint must be declared as a global variable
and make the refernece in the function so python can update that value and not creating a new variable
def Correct(totalPoints):
global totalPoints
print "Correct"
totalPoints = totalPoints+1
print "You have", totalPoints,"points"
You need your Correct
function to return something. Right now, you're printing a result and changing the totalPoints
local variable (which got copied in when passed as an argument to the function). When you make that change to totalPoints
inside of the Correct
function, it's only changing the local, copies variable, not the one that was passed in.
Try this instead:
def Correct(totalPoints):
print "Correct"
totalPoints = totalPoints+1
print "You have", totalPoints,"points"
return totalPoints
Now that it's returning the totalPoints, that variable can be brought back out to your regular program.
1) you're not returning the value from the function 2) global variables are best changed outside of functions - outside of the function, set the variable equal to whatever the function returns or - if you MUST do it inside the function, you want to declare it to be global at the beginning of the function.
def Correct(totalpoints):
return totalpoints += 1
......
if NBAANSWER == NBA2[NBAQues]:
print "Correct!"
totalpoints = Correct(totalPoints)
print "You have ", totalpoints, "points"