Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Mini Project: FizzBuzz

15 min 40 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 7 of 7 · View course roadmap
Step 1

Learn the idea

Time to combine everything — variables, math, conditionals and loops — into the most famous beginner program (and a real interview question!): FizzBuzz.

The rules, for each number from 1 to n:

  • Divisible by 3 and 5 → print FizzBuzz
  • Divisible by only 3 → print Fizz
  • Divisible by only 5 → print Buzz
  • Otherwise → print the number itself

Key insight: check the "divisible by both" case first — if you check 3 alone first, 15 would print Fizz and never reach the FizzBuzz branch. Order of conditions matters.

Where you'll use this

FizzBuzz is a genuine screening question at real companies — interviewers use it because a surprising share of applicants can't order the conditions correctly. You now can, on demand.

Common mistakes

  • Checking % 3 before % 15 — the most specific condition must come first in any if/elif chain.
  • Printing "15"-style strings instead of the number for the default case — print(i), not print("i").

Pro tip

A slicker variant builds the word: word = "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0); print(word or i). Understand why that works (bool × str, empty string is falsy) and you've leveled up.

Step 2

Watch how it runs — line by line


        

Press play to watch Python execute this code, one line at a time.

Step 3

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 4

Pass the challenge +40 XP

Blank · autosaved

Write FizzBuzz for the numbers 1 to 15, printing one result per line.

Target output
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
PYchallenge.py
Run your code to check it…
Step 5

Check your understanding

1. Why must the 'divisible by both' check come first?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path