All Projects
beginner
Number Guessing Game
A fun number guessing game where the computer generates a random number and the player tries to guess it with hints.
Features
- Random number generation
- Hint system (higher/lower)
- Attempt tracking
- Difficulty levels
Technologies Used
random moduleLoopsConditionalsInput handling
Explanation
The game uses the random module to generate a secret number. The player guesses until they find it, receiving "higher" or "lower" hints after each guess.
import random
def guessing_game():
print("=== Number Guessing Game ===")
print("Difficulty: 1=Easy(1-50), 2=Medium(1-100), 3=Hard(1-500)")
difficulty = int(input("Choose difficulty (1-3): "))
ranges = {1: 50, 2: 100, 3: 500}
max_num = ranges.get(difficulty, 100)
max_attempts = {1: 10, 2: 7, 3: 10}[difficulty]
secret = random.randint(1, max_num)
attempts = 0
print(f"\nI'm thinking of a number between 1 and {max_num}")
print(f"You have {max_attempts} attempts. Good luck!\n")
while attempts < max_attempts:
try:
guess = int(input(f"Attempt {attempts + 1}/{max_attempts} - Your guess: "))
attempts += 1
if guess == secret:
print(f"\nš Correct! You guessed it in {attempts} attempts!")
return
elif guess < secret:
print("š Too low! Try higher.")
else:
print("š Too high! Try lower.")
except ValueError:
print("Please enter a valid number!")
print(f"\nš Game over! The number was {secret}")
guessing_game()