DEV Community

Tasib
Tasib

Posted on

Build a Simple Number Guessing Game in Python 🎯 (Beginner Friendly)

Hey devs! πŸ‘‹

Here’s a fun and beginner-friendly project: a Number Guessing Game using Python! 🎯

πŸ’‘ What It Does

  • Randomly picks a number between 1 and 100
  • 7,5,3 attempts(based on your level) to guess the correct number
  • Friendly feedback: πŸ“‰ Too low / πŸ“ˆ Too high
  • Tells you if your guess is too high or too low
  • Includes input validation and emojis for fun!

🧠 The Code

import random

number_to_guess = random.randint(1, 10)
attempts = 5

print("Welcome to the Number Guessing Game!🎯")
print("I'm thinking of a number between 1 and 10.")
level = input("Choose difficulty (easy, medium, hard): ").lower()
if level == "easy":
    attempts = 7
elif level == "medium":
    attempts = 5
elif level == "hard":
    attempts = 3
print(f"You have {attempts} attempts to guess the number.\nGood luck!πŸ€")

for attempt in range(attempts):
    try:
        guess = int(input("Enter your guess: "))
    except ValueError:
        print("Invalid input! Please enter a number.❌")
        continue

    if guess == number_to_guess:
        print("Congratulations! You guessed the number correctly.βœ…")
        break
    elif guess < number_to_guess:
        print("Your guess is too low. Try again.πŸ“‰")
    elif guess > number_to_guess:
        print("Your guess is too high. Try again.πŸ“ˆ")
else:
    print(f"Game over! The correct number was {number_to_guess}❌")
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ GitHub Repository

πŸ”— Check out the full code on GitHub


πŸš€ Future Ideas

  • Add difficulty levels
  • Add a β€œPlay Again” option
  • Track guess history

Hope this helps fellow learners! πŸ’»βœ¨

Feel free to fork and improve!

Leave a comment if you built something similar 😊

Top comments (0)