Untitled

mail@pastecode.io avatar
unknown
plain_text
20 days ago
1.8 kB
4
Indexable
Never
import random

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

def guess_number_game():
    while True:
        random_number = generate_random_number()
        print("Let's play! Guess the number between 1 and 100.")

        attempt = 0

        while attempt < 10:
            try:
                user_number = input("Enter the number: ")

                if user_number.startswith('0'):
                    print("Invalid input. The number cannot start with a zero.")
                    continue

                attempt += 1

                user_number = int(user_number)

                if user_number == random_number:
                    print(f"The guess is correct! You guessed the number in {attempt} attempts.")
                    break
                elif user_number < 1 or user_number > 100:
                    print("The number must be between 1 and 100.")
                elif user_number > random_number:
                    print("The guess is too high!")
                else:
                    print("The guess is too low!")

            except ValueError:
                print("Incorrect value, you need to enter a number between 1 and 100.")

        if attempt == 10:
            print(f"GAME OVER! You have used all 10 attempts. The random number was {random_number}.")

        while True:
            answer = input("Would you like to play another game? Answer with Yes or No: ").strip().lower()

            if answer == "yes":
                break
            elif answer == "no":
                print("Thanks for playing. Goodbye!")
                return
            else:
                print("Please enter Yes or No. ")

if __name__ == '__main__':
    guess_number_game()
Leave a Comment