Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
926 B
3
Indexable
Never
import random

def guess_number():
    # Generate a random number between 1 and 100
    secret_number = random.randint(1, 100)
    attempts = 0
    max_attempts = 5

    print("Welcome to the Guessing Game!")
    print("I've picked a number between 1 and 100. Can you guess it?")

    while attempts < max_attempts:
        try:
            guess = int(input("Enter your guess: "))
        except ValueError:
            print("Invalid input. Please enter a number.")
            continue

        if guess == secret_number:
            print("Congratulations! You guessed the number!")
            break
        elif guess < secret_number:
            print("Too low. Try again.")
        else:
            print("Too high. Try again.")

        attempts += 1

    if attempts == max_attempts:
        print(f"Sorry, you've run out of attempts. The number was {secret_number}.")

if __name__ == "__main__":
    guess_number()
Leave a Comment