A number guessing game is the classic first Python project for a reason. The computer picks a secret number, you guess, and it tells you "too high" or "too low" until you get it right. It is a complete game in about twenty lines, and it teaches you input, loops, conditions, and random numbers — the four building blocks of almost every program.
How does it work? You use random.randint to pick a secret number, then a while loop keeps asking for guesses until the player gets it right. Each guess is compared with the secret, and a counter tracks how many tries it took.
import random
secret = random.randint(1, 100)
tries = 0
print("I picked a number between 1 and 100. Can you guess it?")
while True:
guess = int(input("Your guess: "))
tries += 1
if guess < secret:
print("Too low! Try a bigger number.")
elif guess > secret:
print("Too high! Try a smaller number.")
else:
print(f"You got it in {tries} tries! The number was {secret}.")
break
This game is great for beginners because every line does something you can see. Change the range to 1–1000 and the game gets harder. Add a limit of 7 tries and it becomes a real challenge — with 100 numbers, 7 smart guesses are always enough, which is a neat lesson in binary search hiding inside a game. If you enjoy building small games like this, the Python section on Skill Library has a full beginner-friendly Python course that takes you from your first program to real projects, one day at a time.
When should you build it? Whenever you are learning Python and want your first finished program. You can extend it later: let the player choose the difficulty, keep a high-score file, or build the same game with a graphical interface using Tkinter.
# A harder version: only 7 tries, with a score
import random
secret = random.randint(1, 100)
for attempt in range(1, 8):
guess = int(input(f"Try {attempt}/7: "))
if guess == secret:
print(f"Correct! You won in {attempt} tries.")
break
print("Too low!" if guess < secret else "Too high!")
else:
print(f"Out of tries! The number was {secret}.")
The limits are worth knowing. input() crashes if the player types letters instead of a number, so a real version should check the input with a try/except block. And random is fine for games, but never use it for passwords or security — that needs the secrets module.
Start today: run the game above, play it twice, then add the 7-try limit. When you win on your last guess, you will understand why this little game has taught programming to millions of beginners.
Top comments (0)