Random Number Guessing Game 2

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


def generate_random_number():
        random_number_local = random.randint(1, 100)
        print(f"Random number generated: {random_number_local}")
        return random_number_local


def guess_game_number():
    random_number = generate_random_number()
    attempts = 0
    while attempts < 10:
        try:
            user_guess = int(input("Enter a number between 1 and 100: "))
            if 1 <= user_guess <= 100:
                attempts = attempts + 1
                print(f"Number of guesses {attempts}/10")
                if user_guess == random_number:
                    print("The guess is correct.")
                    break
                elif user_guess > random_number:
                    print("The guess is too high")
                elif user_guess < random_number:
                    print("The guess is too low.")

                if attempts == 10:
                    print("You have reached the maximum number of guesses.")
                    print(f"The random generated number was: {random_number}")
            else:
                print("The number is out of range.")
        except ValueError:
            print("Invalid input. Please enter a number between 1 and 100.")


if __name__ == "__main__":

    print("*** Random Number Guessing Game ***")
    guess_game_number()

    while True:
        answer = input("Do you want to play another game? ")
        match answer:
            case "Yes":
                guess_game_number()
            case "No":
                print("Goodbye")
                break
            case _:
                print("Invalid input. Please enter Yes or No")
Leave a Comment