Майкл Доусон - Программируем на Python 6 глава

2 задача
#Доработайте игру «Отгадай число» из главы З так, чтобы в ней нашла применение функция ask_number().
import random

print("\tWelcome to 'Guess My Number'!")
print("\n\tI'm thinking of a number between 1 and 100.")
print("\n\tTry to guess it in 5 attempts.\n")

def ask_number(question='', low=None, high=None, step=1):
    """Ask for a number within a range."""
    response = None
    while response not in range(low, high, step):
        response = int(input(question))
    return response

the_number = random.randint(1, 100)
guess = int(ask_number("Number is  ", 1, 100))
tries = 1

while guess != the_number:
  if 1<=tries<5 :
    if guess > the_number:
        print("Lower...")
    else:
        print("Higher..."
    guess = int(ask_number("Take a guess: ", 1, 100))
    tries += 1
  else:
      break
 
if guess == the_number:
 print("You guessed it!  The number was", the_number)
 print("And it only took you", tries, "tries!\n")
 input("\n\nPress the enter key to exit.")
else:
  print("Oups,you lose. The number was ", the_number )
  input("\n\nPress the enter key to exit.")

Комментарии

Популярные сообщения из этого блога

Майкл Доусон - Программируем на Python 8 глава задача 1

Майкл Доусон - Программируем на Python 3 глава

Майкл Доусон - Программируем на Python 4 глава