max44
max44

Reputation: 11

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."

Upvotes: 0

Views: 62

Answers (7)

linusg
linusg

Reputation: 6429

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!

Upvotes: 1

mauve
mauve

Reputation: 2763

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"

Upvotes: 0

sabbahillel
sabbahillel

Reputation: 4425

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."

Upvotes: 0

nephlm
nephlm

Reputation: 553

totalPoints is passed by value. Which means a new copy is made and that copy is manipulated in the Correct function. Since the modified totalPoints is never returned it is lost. You need something like:

def Correct(totalPoints):
    return totalPoints += 1

and call it with

totalPoints = Correct(totalPoints)

The code doesn't show the original totalPoint definition, but that should get you going in the right direction.

Upvotes: 0

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"

Upvotes: 0

bobbyz
bobbyz

Reputation: 5046

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.

Upvotes: 0

Eli
Eli

Reputation: 38929

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.

Upvotes: 0

Related Questions