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

后端 未结 7 1146
余生分开走
余生分开走 2021-01-27 19:55

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

def Correct(totalPoints):
    print \"Correct\"
    totalPoints = totalPoints+1
          


        
7条回答
  •  暖寄归人
    2021-01-27 20:17

    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.

提交回复
热议问题