Untitled

mail@pastecode.io avatar
unknown
python
11 days ago
1.6 kB
4
Indexable
Never
import random


def generate_random_number():
    random_number = random.randint(1, 100)
    return random_number


def guess_number_game():
    x = 0
    random_number = generate_random_number()
    while x < 10:
        try:
            user_entry = int(input("Guess the number from 1 to 100:"))
            if user_entry == random_number or random_number < user_entry < 101 or 0 < user_entry < random_number:
                x = x + 1
            if user_entry == random_number:
                print(f"Good job! Number of guesses: {x}")
                break
            elif random_number < user_entry < 101:
                print("Overshot the mark!")
            elif 0 < user_entry < random_number:
                print("Undershot the mark!")
            elif 1 > user_entry or user_entry > 100:
                print("Invalid Input! Please input a number from 1 to 100!")
            if x == 10:
                print("Sorry, no more guesses!")
                print(f"Random number was: {random_number}")
                break
        except ValueError:
            print("Invalid Input! Please input a number from 1 to 100!")


if __name__ == "__main__":
    game_to_be_done = 1
    while game_to_be_done == 1:
        guess_number_game()
        new_game_prompt = input("Would you like to start a new game? y/n ")
        new_game_prompt = new_game_prompt.strip()
        if new_game_prompt == "y":
            game_to_be_done = 1
        else:
            game_to_be_done = 0
    else:
        print("Game Over")
Leave a Comment