Untitled
import random def number_guessing_game(): print("Welcome to the Number Guessing Game!") print("I have chosen a number between 1 and 100. Try to guess it!") # Secret number is randomly selected secret_number = random.randint(1, 100) attempts = 0 while True: # Get player's guess try: guess = int(input("Enter your guess: ")) except ValueError: print("Please enter a valid number.") continue attempts += 1 # Check if the guess is correct if guess < secret_number: print("Too low! Try again.") elif guess > secret_number: print("Too high! Try again.") else: print(f"Congratulations! You've guessed the number {secret_number} in {attempts} attempts.") break # Start the game number_guessing_game()
Leave a Comment