Modify the value of a variable

1

I'm wanting to make a game of Stone, Paper or Scissors in Python, nothing complicated. The player starts with 2 points and depending on whether he wins or loses adds or subtracts value. The problem is that it is not saved in the variable that I declare first, so even if it reaches 0 points the while it is still running.

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)
    
asked by Lucas. D 27.10.2016 в 02:25
source

1 answer

1

The variable that each function receives is a copy of the value, with which you are not modifying the variable outside the local scope. If you want to modify the outside one, it is convenient for you not to pass it "points" as a parameter, and you should use it at the beginning of each function:

def piedra():
    global puntos  # A partir de aquí, podes acceder a la variable que declaraste al inicio del módulo. El codigo sigue igual como lo tenes.

Look if it works for you:)

    
answered by 27.10.2016 / 02:47
source