Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 20 of 70

Python control flow practice: five small programs

Practise Python if statements and loops by building five small programs, from a FizzBuzz variation to a prime checker, with real output and worked solutions.

15 minControl flowUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCombine conditions, while loops, for loops, break and nested loops to solve small problems on your own.

After this lesson you will have solved five small problems that combine everything in this section: conditions, while, for, range(), break, continue and nested loops. The real learning here comes from trying each problem yourself before you read the solution.

How to use this lesson

For each task, read the problem and cover the solution. Write down what goes in, what should come out and the steps in between. Then write the code in a file and run it. Only after that, compare with the solution. If yours is different but gives the same output, that is not a mistake. There are many right answers.

When something goes wrong, read the last line of the error, check indentation and colons, and add a temporary print to see the values.

Task 1: Fizz and buzz for a shop

Count from 1 to 15. For multiples of 3 print fizz, for multiples of 5 print buzz, for multiples of both print fizzbuzz, and otherwise print the number itself.

Think about the order of the conditions. Which check must come first?

python
for number in range(1, 16):
    if number % 15 == 0:
        print("fizzbuzz")
    elif number % 3 == 0:
        print("fizz")
    elif number % 5 == 0:
        print("buzz")
    else:
        print(number)
result
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz

The % operator gives the remainder after division, so number % 3 == 0 means "divisible by 3". Multiples of 15 are also multiples of 3 and 5, so that test goes first. This is the order trick from the if lesson.

Task 2: Sum of the valid marks

A teacher records marks as a list, using -1 for a student who was absent. Calculate the average of the real marks.

python
marks = [72, -1, 85, 64, -1, 90]
total = 0
count = 0
for mark in marks:
    if mark == -1:
        continue
    total += mark
    count += 1
print("Marks counted:", count)
print("Average:", total / count)
result
Marks counted: 4
Average: 77.75

continue skips the absent students. Two variables are kept: the total, and the count of real marks. Dividing by the count, not by the length of the list, avoids counting the absent students.

Task 3: Doubling savings

A savings account starts at 1000 and grows by 10 percent each year. How many years until it is at least 2000?

The number of years is not known in advance, so this is a while loop.

python
balance = 1000
years = 0
while balance < 2000:
    balance = balance * 1.10
    years += 1
print("Years:", years)
print("Balance:", round(balance, 2))
result
Years: 8
Balance: 2143.59

Each pass adds a year and applies the growth. The loop stops as soon as the balance reaches 2000 or more.

Task 4: Is it a prime number?

A prime number is greater than 1 and divisible only by 1 and itself. Write a check for numbers from 2 to 20 that prints the primes.

python
for candidate in range(2, 21):
    for divisor in range(2, candidate):
        if candidate % divisor == 0:
            break
    else:
        print(candidate, "is prime")
result
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime

This uses a nested loop and the loop else. For each candidate, the inner loop tries every divisor from 2 up to one less than the candidate. If any divides evenly, break ends the search and the else is skipped. If the loop finishes without break, no divisor was found, so the number is prime.

Task 5: A multiplication grid with a limit

Print a multiplication table for 1 to 4, but stop printing numbers in a row as soon as a product goes above 8, then move on to the next row.

python
for row in range(1, 5):
    for column in range(1, 5):
        product = row * column
        if product > 8:
            break
        print(product, end=" ")
    print()
result
1 2 3 4 
2 4 6 8 
3 6 
4 8 

break leaves only the inner loop, and the empty print() then ends the line. The rows 3 and 4 are cut short.

Going further

Try changing each program. Make Task 1 count to 30. Make Task 2 use a different sentinel. Change the interest rate in Task 3. Print only primes below 50 in Task 4. Each change tests whether you really understand the code.

Common mistakes

  • Starting with code instead of a plan. A few lines of notes save a lot of confusion.
  • Wrong order of conditions. The specific case must come before the general one.
  • Forgetting the update in a while loop. Check that the condition can change.
  • Off-by-one errors. range(1, 16) stops at 15. Say out loud which numbers you expect.
  • Copying a solution without understanding it. Retype it from memory the next day.
  • Dividing by zero. If every mark were absent, count would be 0 and Task 2 would fail. Real programs check for that case.

Exercise

Write a program that goes through the numbers 1 to 30. It should print only the numbers divisible by 4, but stop completely as soon as it reaches a number divisible by 13 that is greater than 20, and print how many numbers it printed.

Show one possible answer
python
printed = 0
for number in range(1, 31):
    if number > 20 and number % 13 == 0:
        break
    if number % 4 == 0:
        print(number)
        printed += 1
print("Numbers printed:", printed)
result
4
8
12
16
20
24
Numbers printed: 6

The stopping test is placed before the printing test, so the loop leaves before it considers the number 26. The multiples of 4 below that are printed first.

Next: the Working with text section.

Key points

  • Most small programs combine a loop to go through data with a condition to decide what to do.
  • Plan first: write down the input, the steps and the expected output before coding.
  • Test with tiny cases you can check by hand, then try the edge cases.
  • Reading other people's solutions is useful only after you have attempted the problem yourself.

Check yourself

1. What is a good first step when you face a new problem?
2. You need to count how many numbers in a list are even. What pattern fits?
3. Why test with tiny cases first?
4. A program needs to stop at the first match. Which keyword helps?