Random Number Guessing Game

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


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


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

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


print("*** Random Number Guessing Game ***")
random_number = generate_random_number()
guess_game_number(random_number)
Leave a Comment