Witam. Napisałem dwa takie same programy. Kody są niemal identyczne.
# jaka to liczba nowa edycja
import random
def powitanie():
print(
"""
Witam w grze 'Jaka to liczba'
Twoim zadaniem jest odgadnięcie liczby
wylosowanej przez komputer
""")
def yes_or_no(question):
response = None
while response not in("t", "n"):
response = input(question).lower()
return response
def ask_number(question, low, high):
response = None
while response not in range(low, high):
response = int(input(question))
return response
def main():
powitanie()
yes_or_no("Czy jesteś gotowy?")
guess = ask_number("Podaj liczbę: ", 1, 100)
tries = 10
number = random.randint(1, 100)
while guess != number:
if tries == 0:
break
if guess < number:
print("Za mała...")
elif guess > number:
print("Za duża...")
else:
print("Zgadłeś!")
guess = ask_number("Podaj liczbę: ", 1, 100)
tries -= 1
if guess == number:
print("Zgadłeś. Chodziło mi o liczbę: ", guess)
elif tries == 0:
print("Wyczerpałeś wszystkie próby!")
main()
input("\n\nAby zakończyć program naciśnij enter.")
## NIEDZIAŁAJĄCY! POWTARZANIE FUNCKJI YES_OR_NO
# Jaka to liczba nowa próba
import random
def powitanie():
print(
"""
Witaj w programie 'Jaka to liczba'
""")
def yes_or_no(question):
response = None
while response not in ('t', 'n'):
respone = input(question).lower()
return response
def ask_number(question, low, high):
response = None
while response not in range(low, high):
response = int(input(question))
return response
def main():
powitanie()
yes_or_no("Czy jesteś gotowy?")
guess = ask_number("Podaj liczbę: ", 1, 100)
tries = 10
number = random.randint(1, 100)
while guess != number:
if tries <= 0:
break
if guess < number:
print("Za mała...")
elif guess > number:
print("Za duża...")
else:
print("Zgadłeś!")
guess = ask_number("Podaj liczbę: ", 1, 100)
tries -= 1
if guess == number:
print("Zgadłeś! Chodziło mi o liczbę: ", guess)
elif tries <= 0:
print("Skończyły ci się próby!")
main()
input("\n\nAby zakończyć naciśnij enter.")
Ten pierwszy kod działa. Drugi się wiesza przy funkcji yes_or_no. Są identyczne. Czy nie widzę błedu?
@Edit
Napisałem kod jeszcze raz i wszystko gra. Nie wiem co jest grane.