I tried to program a rock paper scissors game in which I will count points, but in every answer the points are resetting back to zero. What am I doing wrong? Here is the code
import random
points=0
oppoints=0
opt=["rock","papper","scissors"]
ans="yes"
def result(name,win,loose,points,oppoints):
if cho==name:
if opp==win:
points += 1
print("congrats you got a point")
elif opp==loose:
oppoints += 1
print("you lost the point")
else :
print("its a tie no one gets a point")
print("points" ,points)
print("oppoints" ,oppoints)
while ans == "yes" :
while points < 2 or oppoints < 2 :
cho=input("rock papper scissors: ")
opp=random.choice(opt)
print("opponent: " +opp)
result("rock","scissors","papper",points,oppoints)
result("papper","rock","scissors",points,oppoints)
result("scissors","papper","rock",points,oppoints)
if points>oppoints :
print("you win!!")
else:
print("you lost :(")
ans=input("wanna play again?? ")
if ans == "yes":
points=0
oppoints=0
print("good bye")
the points
variable in your results
function is seen as a new local variable and only changed in the scope of the function. It's the typical "pass by reference vs value" issue.
If you want to change the value outside of the function, you can, e.g. return the new points.
def result(name,win,loose,points,oppoints):
if cho==name:
if opp==win:
points += 1
print("congrats you got a point")
elif opp==loose:
oppoints += 1
print("you lost the point")
else :
print("its a tie no one gets a point")
print("points" ,points)
print("oppoints" ,oppoints)
return points, oppoints
and then call with:
points, oppoints = result("rock","scissors","papper",points,oppoints)