Statement
Solution
Python Exercise 2.3: Leap Year Checker
Create a program that asks the user for a year and determines whether it is a Leap Year or not.
The Rules:
- If a year is divisible by 4, it might be a leap year.
- BUT, if that year is divisible by 100, it is NOT a leap year...
- UNLESS the year is also divisible by 400.
Example: 2000 was a leap year (divisible by 400), but 1900 was not (divisible by 100 but not 400).
Sample Interaction:
Input
Output
2024
Enter a year: 2024
--------------------
2024 is a Leap Year
1900
Enter a year: 1900
--------------------
1900 is NOT a Leap Year
Solution
We can solve this using logical operators and & or to combine the rules into a single condition.
# 1. Get User Input
year = int(input("Enter a year: "))
print("-" * 20)
# 2. Check Leap Year Rules
# Logic: (Divisible by 4 AND NOT 100) OR (Divisible by 400)
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a Leap Year")
else:
print(f"{year} is NOT a Leap Year")
Key Concepts:
and: Both conditions must be true.or: At least one condition must be true.% 4 == 0checks if the year divides evenly by 4.!=means "not equal to".
Test Console
Run code to see output...