Untitled
import random def guessing_game(): print("Welcome to the Guessing Game!") print("I've selected a random number between 1 and 100. Your task is to guess it.") # Set the random number secret_number = random.randint(1, 100) # Set the maximum number of attempts max_attempts = 10 attempts = 0 while attempts < max_attempts: try: # Get the player's guess guess = int(input("Enter your guess: ")) # Check if the guess is correct if guess == secret_number: print(f"Congratulations! You guessed the correct number {secret_number} in {attempts + 1} attempts.") break elif guess < secret_number: print("Too low! Try again.") else: print("Too high! Try again.") attempts += 1 except ValueError: print("Invalid input. Please enter a valid number.") else: print(f"Sorry, you've run out of attempts. The correct number was {secret_number}.") if __name__ == "__main__": guessing_game()
Leave a Comment