Modify global variables

1

I was doing a very simple program that consists of the user choosing a number between one and one hundred and the computer tries to guess it.

If the computer does not guess, you should tell it if the number you chose is a number higher or lower than yours and in this way the computer modifies the values of randint .

The problem is that, for example, when the computer does not guess and you write "lower" does not change the value mini (the minimum between one and one hundred to find a number). I already realized what happened: When I want to change the values mini and maxi the interpreter takes them as local values and does not modify the global values.

My question is: How can I make the values change?

Here is the code:

import random
print("Te voy a explicar las reglas del juego: Tu escojes un número del 1  al 100. La computadora intenta adivinar tu número. Si tu por ejemplo escojiste el 15 y la computadora escojió el 10 debes decirle: \"más alto\" y si dice 34 debes decirle: \"más bajo\"")

adivina = int(input("Vamos a jugar :D. Elige un número del 1 al 100: "))

def adivinador():
    while True:
        adivinanza = random.randint(mini, maxi)
        print("Es:" + " " + str(adivinanza) + " " + "el número que has escogido?")
        respuesta = input("Si/No: ")
        intentos = 0
        mini = 1
        maxi = 100        
        print(mini)
        print(maxi)
        print(adivinanza)
        if respuesta == "Si":
            print("Bieeeen he ganado :D" + " " + "y en solo" + " " + str(intentos) + " " + "intentos")
            break
        elif respuesta == "No":
            print("Mi número es más bajo o más alto que el tuyo?")
            min_max = input("mas bajo/ mas alto: ")
            if min_max == "mas bajo":
                mini = adivinanza
                intentos += 1
            elif min_max == "más alto":
                maxi = adivinanza
                intentos += 1
            else:
                print("Estoy confundido.")

adivinador()
    
asked by MahaSaka 21.09.2017 в 22:43
source

1 answer

1

You do not need to use global variables. It is not a problem of scope, what happens is that simply in each iteration of while you return to restart the values of mini , maxi e intentos . The only thing you have to do is remove them from while , but they will remain local variables of the function and only that function can access and modify:

def adivinador():
    intentos = 0
    mini = 1
    maxi = 100   
    while True:
        adivinanza = random.randint(mini, maxi)
        print("Es:" + " " + str(adivinanza) + " " + "el número que has escogido?")
        respuesta = input("Si/No: ")

        if respuesta == "Si":
            print("Bieeeen he ganado :D" + " " + "y en solo" + " " + str(intentos) + " " + "intentos")
            break
        elif respuesta == "No":
            print("Mi número es más bajo o más alto que el tuyo?")
            min_max = input("más bajo/ más alto: ")
            if min_max == "más bajo":
                mini = adivinanza + 1
                intentos += 1
            elif min_max == "más alto":
                maxi = adivinanza - 1
                intentos += 1
            else:
                print("No me ayudas mucho... :(") 
        else:
            print("No me ayudas mucho... :(") 

Be careful with the input , you must enter exactly the same. You have "higher" with tilde but the print of input asks "higher".

One thing to keep in mind is that random.randint returns a pseudo-random integer between the two numbers given, including these . So that there is no possibility that you ask again for the same number, you must take this into account, so you subtract one from maxi and add one to mini when you modify them.

Since you mention it, although it is not your real problem, to access a global variable (defined at the module level) the reserved word global is used. This is only necessary to reassign a new object, not to read or access its methods.

n = 0
print("Valor de variable n global antes de ejecutar foo: ",  n)

def foo(m):
    global n
    n = n + m
    print("Valor de n dentro de foo: ",  n)
foo(4)
print("Valor de variable n global trás ejecutar foo: ",  n)
    
answered by 21.09.2017 / 22:58
source