Untitled
import random def guess_number_game(): print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100.") # Generate a random number between 1 and 100 number_to_guess = random.randint(1, 100) attempts = 0 max_attempts = 10 while attempts < max_attempts: try: guess = int(input("Make a guess: ")) except ValueError: print("Please enter a valid number.") continue attempts += 1 if guess < number_to_guess: print("Too low!") elif guess > number_to_guess: print("Too high!") else: print(f"Congratulations! You guessed the number {number_to_guess} in {attempts} attempts.") break else: print(f"Sorry, you've used all your attempts. The number was {number_to_guess}.") if __name__ == "__main__": guess_number_game()
Leave a Comment