← All coding practice

Coding Practice · Beginner · October 20, 2026 · 5 min read

Python Coding Exercise #48: The Next Leap Year

Part of the series Python Practice Exercise — part 48

Before you start: Know while True and break · Know the % operator · Know the leap year rule

The question

Please write a program which asks the user for a year, and prints out the next leap year.

Year: 2023
The next leap year after 2023 is 2024

If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:

Year: 2024
The next leap year after 2024 is 2028

What is new here: searching with a loop

In this exercise, we don’t know how many years the program has to look through. From 2023, the answer is one year: 2024. From 2024, it is four years: 2028. From 1896, it is eight years: 1904.

When you don’t know how many steps you need, a loop can search: check one condition, and if it isn’t True, move to the next. The pattern is:

  1. Start at the first possible answer.
  2. If it is True, stop (break).
  3. If it isn’t, move to the next one and repeat.

Here, the “possibilities” are the years after the one the user typed.

Before coding: two things to decide

Where to start. The exercise asks for the next leap year after the year entered, even if that year is itself a leap year. For 2024, the answer is 2028, not 2024. So the search starts at year + 1, not at year.

How to test a leap year. This is the same rule as in Exercise #40:

  • A year divisible by 400 is a leap year (such as 2000).
  • A year divisible by 4 is a leap year, unless it is divisible by 100 (so 1900 and 2100 are not).

My Solution

year = int(input("Year: "))
candidate = year + 1

while True:
    if candidate % 400 == 0 or (candidate % 4 == 0 and candidate % 100 != 0):
        print(f"The next leap year after {year} is {candidate}")
        break
    else:
        candidate += 1

How My Solution Works

1. Two variables: the user inputs and the next year we have to find

year keeps the number the user typed and never changes, because the final message has to say “after 2023”. candidate is the year being tested, and it starts at year + 1. If the program changed year itself, it would lose the user input.

2. The leap year test is one condition

candidate % 400 == 0 or (candidate % 4 == 0 and candidate % 100 != 0) is true for a leap year. It has two parts joined by or:

  • candidate % 400 == 0 accepts years like 2000 and 2400.
  • candidate % 4 == 0 and candidate % 100 != 0 accepts the normal leap years like 2024 and 2028, but not the centuries like 1900 and 2100.

3. The printing line of code

When the condition is true, the program prints the message with the original year and the candidate it found, and break ends the loop. (See Exercise #43 for break.)

4. Any other year moves on to the next

When the condition is false, candidate += 1 moves to the next year, and the loop tests that one.

Following the samples

Year enteredcandidate tested, in orderResult
20232024 (leap)The next leap year after 2023 is 2024
20242025, 2026, 2027, 2028 (leap)The next leap year after 2024 is 2028

University Solution

start_year = int(input("Year: "))
year = start_year + 1
while True:
    if year % 100 == 0:
        if year % 400 == 0:
            break
    elif year % 4 == 0:
        break

    year += 1

print(f"The next leap year after {start_year} is {year}")

The University Solution searches in the same way, with three differences:

  • The names are the other way round. start_year is the number the user typed, and year is the one being tested. My Solution calls them year and candidate.
  • The test is split into two levels. Instead of one long condition, it first asks whether the year is a century (year % 100 == 0). If it is, the year is a leap year only when it is divisible by 400. If it isn’t, the year is a leap year when it is divisible by 4.
  • The message is printed after the loop. The loop only finds the year, and a single print() below it shows the answer. In My Solution, the print() is inside the loop, just before the break.

Reading the nested if

The elif in the University Solution belongs to the outer if year % 100 == 0, not to the inner one. That’s what makes it work:

The year isOuter if year % 100 == 0ThenLeap year?
2000trueyear % 400 == 0 is trueYes, break
1900trueyear % 400 == 0 is falseNo, goes to year += 1
2024falseelif year % 4 == 0 is trueYes, break
2023falseelif year % 4 == 0 is falseNo, goes to year += 1

When neither break runs, Python reaches year += 1 and moves on to the next year. Note that year += 1 line is not inside the if, so it runs for every year that isn’t a leap year.

Both programs gave the correct next leap year for every year from 1890 to 2109, plus the edge cases 2399, 2400 and 3000.

Things to notice

Century years are skipped. From 1896, the next leap year is 1904, not 1900, because 1900 is divisible by 100 but not by 400. The program checks 8 more years. Likewise, from 2096 the answer is 2104, and from 1999 it is 2000, because 2000 is divisible by 400. This is the longest wait you can get: never more than 8 years.

If the year typed is a leap year, it is skipped. That’s what year + 1 does. Starting at the year itself would print The next leap year after 2024 is 2024, which is wrong.

The loop always ends. There is a leap year at least every 8 years, so the search always finds an answer and break always runs.

Key takeaways

  • A loop can search: test one condition, and if it doesn’t fit, move on to the next.
  • Start the search at year + 1 when the answer must come after the year given.
  • Keep the original value in its own variable, so the final message can use it.
  • A long condition can be written as one line with and / or, or split into nested if statements. Both give the same result.
  • An elif belongs to the if at the same indentation, so its position decides which question it answers.
  • Century years are leap years only when they are divisible by 400.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · loops · while-loop · break · leap-year · modulo