Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
877 B
2
Indexable
Never
import random

def display_heading():
    print("Guess the number!")

def play_game(limit):
    random_number = random.randint(1, limit)
    print(f"I'm thinking of a number from 1 to {limit}")

    tries = 0
    while True:
        user_guess = int(input("Your guess: "))
        tries += 1

        if user_guess < random_number:
            print("Too low.")
        elif user_guess > random_number:
            print("Too high.")
        else:
            print(f"You guessed it in {tries} tries.")
            break

    return tries

def main():
    display_heading()

    play_again = 'y'
    while play_again.lower() == 'y':
        limit = int(input("Enter the limit: "))
        tries = play_game(limit)

        play_again = input("Would you like to play again? (y/n): ")

    print("Bye!")

if __name__ == "__main":
    main()