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()