<aside> 🎨 Hi, I'm Sanjai Kumaren, a School student.complet my python
</aside>
**I study at a local high school where I'm focusing on computer science and mathematics. My passion for technology led me to learn Python programming, which I recently completed. I'm particularly interested in web development and data analysis, and I'm excited to continue building my skills in these areas**
import random
import time
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
time.sleep(1)
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 10
# Game difficulty settings
difficulty = input("Choose difficulty (easy/medium/hard): ").lower()
if difficulty == "easy":
max_attempts = 10
elif difficulty == "medium":
max_attempts = 7
elif difficulty == "hard":
max_attempts = 5
else:
print("Invalid choice. Setting to medium difficulty.")
max_attempts = 7
print(f"You have {max_attempts} attempts to guess the number.")
# Main game loop
while attempts < max_attempts:
try:
# Get user's guess
guess = int(input("\\nEnter your guess: "))
attempts += 1
# Check the guess
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print(f"\\nCongratulations! You guessed the number {secret_number} in {attempts} attempts!")
return
# Show remaining attempts
remaining = max_attempts - attempts
if remaining > 0:
print(f"Attempts remaining: {remaining}")
except ValueError:
print("Please enter a valid number.")
print(f"\\nGame over! You've used all {max_attempts} attempts.")
print(f"The secret number was {secret_number}.")
if __name__ == "__main__":
number_guessing_game()
# Ask if the player wants to play again
while input("\\nDo you want to play again? (yes/no): ").lower().startswith('y'):
number_guessing_game()
print("Thanks for playing!")
This is a game I made in my school days
Here's what the output of this code would look like when run:
Welcome to the Number Guessing Game! I'm thinking of a number between 1 and 100. Choose difficulty (easy/medium/hard): medium You have 7 attempts to guess the number.
Enter your guess: 50 Too high! Attempts remaining: 6
Enter your guess: 25 Too low! Attempts remaining: 5
Enter your guess: 37 Too low! Attempts remaining: 4
Enter your guess: 45 Too high! Attempts remaining: 3
Enter your guess: 42 Too high! Attempts remaining: 2
Enter your guess: 40 Too high! Attempts remaining: 1
Enter your guess: 38 Too low! Game over! You've used all 7 attempts. The secret number was 39.
Do you want to play again? (yes/no): no Thanks for playing!
This is just one possible outcome since the game generates a random number each time it's played. The player tries to guess the number while receiving "too high" or "too low" hints, with a limited number of attempts based on the chosen difficulty level.