Number Guessing Game

Python Projects — Number Guessing Game
Python Projects (Beginner)
Course 4 · Chapter 1 · Number Guessing Game

🎯 Number Guessing Game

Welcome to Python Projects — a different format from Courses 1-3. Instead of a topic followed by challenges, each chapter here builds one complete, playable project end to end, scoped to Fundamentals-level skills. First up: a number guessing game, using nothing beyond loops, conditionals, and the random module — all from Course 1.

🎮 What We're Building

A simple, genuinely fun command-line game: the program secretly picks a random number between 1 and 100, the player guesses repeatedly, and after each guess the program says "too high," "too low," or "correct" — ending with how many attempts it took.

Step 1: Picking a Random Number

import random secret_number = random.randint(1, 100)

random.randint(1, 100) reuses the exact function from Course 1, Chapter 9's own dice-roller challenge — inclusive on both ends, so it can return anything from 1 through 100.

Step 2: The Guessing Loop

while True: guess = int(input("Guess a number between 1 and 100: "))

An infinite while True loop (Course 1, Chapter 4) — the game keeps asking for guesses until something inside the loop explicitly breaks out of it. input() always returns a string, so int(...) wraps it immediately to get a real number to compare.

Step 3: Giving Feedback

if guess < secret_number: print("Too low!") elif guess > secret_number: print("Too high!") else: print("Correct! 🎉") break

Plain if/elif/else comparisons (Course 1, Chapter 3) — the break only fires on a correct guess, exiting the while True loop from Step 2 at exactly that point.

Step 4: Counting Attempts & Ending the Game

attempts = 0 while True: guess = int(input("Guess a number between 1 and 100: ")) attempts += 1 if guess < secret_number: print("Too low!") elif guess > secret_number: print("Too high!") else: print(f"Correct! You got it in {attempts} attempts. 🎉") break

attempts is the same running-total accumulator pattern from Course 1, Chapter 4 — initialized to 0 outside the loop, incremented by 1 on every pass through it, then read once the loop finally ends.

🏁 The Complete Game

import random secret_number = random.randint(1, 100) attempts = 0 print("I'm thinking of a number between 1 and 100.") while True: guess = int(input("Your guess: ")) attempts += 1 if guess < secret_number: print("Too low!") elif guess > secret_number: print("Too high!") else: print(f"Correct! You got it in {attempts} attempts. 🎉") break

That's a complete, playable game in 15 lines — every piece is something already covered in Course 1, combined into something genuinely fun to run rather than a standalone exercise.

⚠ A Non-Numeric Guess Crashes This Version

Typing "forty" instead of a number raises a ValueError from int(...), crashing the whole program — this version doesn't validate input yet. Course 2, Chapter 3's try/except ValueError is exactly the fix, and it's one of this chapter's own suggested extensions below.

random.randint()

Picks the secret number, inclusive on both ends.

while True + break

Loops until the correct guess is made, then exits.

if/elif/else

Compares the guess to the secret number and gives a hint.

Accumulator pattern

attempts counts guesses across every pass through the loop.

🚀 Extend This Project

Try these on your own — no solutions provided, just like a real side project:

  • Wrap the int(input(...)) line in a try/except ValueError (Course 2, Chapter 3) so a non-numeric guess prints a friendly message and asks again, instead of crashing.
  • Add a difficulty selector at the start (easy = 1-50, hard = 1-500), changing the random.randint() range based on the player's choice.
  • After the player wins, ask if they want to play again — wrap the entire game in an outer loop, reusing the loop-inside-a-loop pattern from Course 2, Chapter 5's nested comprehensions (conceptually, not literally a comprehension here).
  • Give up after 10 attempts instead of looping forever — track attempts against a limit and break with a "you lose" message if it's exceeded.

🎯 What's Next

Next chapter: Simple Calculator — functions and basic error handling.