Untitled
import random def guess_the_number(): print("Welcome to Guess the Number!") print("I'm thinking of a number between 1 and 100.") # Generate a random number between 1 and 100 number = random.randint(1, 100) attempts = 0 guessed = False while not guessed: # Ask the user to guess guess = int(input("Enter your guess: ")) attempts += 1 if guess < number: print("Too low! Try again.") elif guess > number: print("Too high! Try again.") else: print(f"Congratulations! You guessed the number in {attempts} attempts.") guessed = True # Run the game guess_the_number()
Leave a Comment