I'm wanting to make a Rock, Paper, Scissors game in Python, nothing complicated. The player starts with 2 points and depending on whether he wins or loses, value is added or subtracted. The problem is that it is not saved in the variable that I declare first, so that even if it reaches 0 points, the while continues to execute.
from random import randint
pi = "Piedra"
pa = "Papel"
ti = "Tijera"
puntos = 2
while puntos > 0:
elemento = input("¿Piedra, papel o tijera? : ")
maquina = randint(0,2)
def piedra(puntos):
if elemento.lower() == "piedra" and maquina == 0:
print ("Empate, la maquina eligio ", pi , " ! ")
elif elemento.lower() == "piedra" and maquina == 1:
print ("Perdiste, la maquina eligio ", pa, " ! ")
puntos -= 2
print ("Tienes " , puntos , " puntos.")
elif elemento.lower() == "piedra" and maquina == 2:
print ("Ganaste, la maquina eligio ", ti, " ! ")
puntos += 2
print ("Tienes " , puntos , " puntos.")
def papel(puntos):
if elemento.lower() == "papel" and maquina == 0:
print ("Ganaste, la maquina eligio ", pi, " ! ")
print ("Tienes " , puntos , " puntos.")
puntos += 2
elif elemento.lower() == "papel" and maquina == 1:
print ("Empate, la maquina eligio ", pa, " ! ")
elif elemento.lower() == "papel" and maquina == 2:
print ("Perdiste, , la maquina eligio " , ti, " ! ")
puntos -= 2
print ("Tienes " , puntos , " puntos.")
def tijera(puntos):
if elemento.lower() == "tijera" and maquina == 0:
print ("Perdiste, la maquina eligio " , pi, " ! ")
puntos -= 2
print ("Tienes " , puntos , " puntos.")
elif elemento.lower() == "tijera" and maquina == 1:
print ("Ganaste, la maquina eligio " , pa, " ! ")
print ("Tienes " , puntos , " puntos.")
puntos += 2
elif elemento.lower() == "tijera" and maquina == 2:
print ("Empate, la maquina eligio " , ti, " ! ")
piedra(puntos)
papel(puntos)
tijera(puntos)
The variable that each function receives is a copy of the value, so you are not modifying the variable outside the local scope. If you want to modify the outside one, you might want to not pass it "dots" as a parameter, and you should use at the beginning of each function:
See if it works for you :)