Minha solução ficou um pouco diferente:
import random
LIMITE_MAXIMO = 100
def escolher_numero():
return random.randint(1,LIMITE_MAXIMO)
def coletar_input():
try:
chute = input(f"Tente adivinhar o número (1-{LIMITE_MAXIMO}) ou digite sair: ").lower()
valor = int(chute)
if valor < 1 or valor > LIMITE_MAXIMO:
print(f"Atenção! o numero deve estar entre 1 e {LIMITE_MAXIMO}.")
return None
return valor
except ValueError:
if chute != "sair":
print("Ops! Isso não é um numero valido")
return None
else:
return 0
def validar_tentativa():
numero = escolher_numero()
tentativas = 0
while True:
tentativa = coletar_input()
if tentativa is not None and tentativa > 0:
tentativas+=1
if tentativa > numero:
print("Muito alto! Tente novamente:")
elif tentativa < numero:
print("Muito baixo! Tente novamente:")
else:
print(f"Parabéns! voce acertou o numero {numero} com {tentativas} tentativas")
continuar = input("Deseja jogar novamente? (s/n): ").lower()
if continuar != "s":
break
else:
tentativas = 0
numero = escolher_numero()
elif tentativa == 0:
print(f"voce tentou {tentativas} e o numero secreto era {numero}")
break
validar_tentativa()