Introduction
In this project, you will learn how to determine whether a given year is a leap year or a common year. You will implement a Python function that takes a year as input and outputs whether the year is a leap year or a common year.
👀 Preview
Please enter a year: 2000
>>> The year is a leap year
Please enter a year: 1900
>>> The year is a common year
Please enter a year: 1582
>>> Please enter a year after 1582
🎯 Tasks
In this project, you will learn:
- How to implement a function to check if a year is a leap year or a common year
- How to handle different types of input, including valid years, invalid years, and non-integer inputs
- How to print the appropriate output based on the input year
🏆 Achievements
After completing this project, you will be able to:
- Understand the concept of leap years and common years
- Write a Python function to determine the type of a given year
- Implement error handling to handle various input scenarios
- Apply your knowledge of conditional statements and data types in Python
Implement the leap_year Function
In this step, you will learn how to implement the leap_year function in the leap_year.py file.
- Open the
leap_year.pyfile in your preferred code editor. - Inside the
leap_yearfunction, add the following code:
def leap_year() -> None:
"""
Determine whether a given year is a leap year or a common year.
The function prompts the user to enter a year and checks if it is a leap year or a common year.
Returns:
None
"""
year = input("Please enter a year: ")
try:
year = int(year)
except ValueError:
print("Please enter a valid year!")
return
if year < 1582:
print("Please enter a year after 1582")
return
elif year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print("The year is a leap year")
else:
print("The year is a common year")
This function first prompts the user to enter a year. It then checks if the input is a valid integer. If the input is not a valid integer, it prints "Please enter a valid year!" and returns.
If the input year is before 1582, it prints "Please enter a year after 1582" and returns.
If the year is divisible by 4 but not by 100, or if the year is divisible by 400, it is a leap year, and the function prints "The year is a leap year". Otherwise, it is a common year, and the function prints "The year is a common year".
- Save the
leap_year.pyfile.
Run the leap_year Function
In this step, you will learn how to run the leap_year function and test it with different input values.
- Open a terminal or command prompt and navigate to the directory where the
leap_year.pyfile is located. - Run the
leap_yearfunction by executing the following command:
python3 leap_year.py
The program will prompt you to enter a year. Try entering different values, such as:
- A valid leap year (e.g., 2000)
- A valid common year (e.g., 1900)
- A year before 1582 (e.g., 1500)
- A non-integer value (e.g., "hello")
Observe the output and ensure that the function behaves as expected for each input.
Verify that the function correctly identifies leap years and common years, and that it handles invalid inputs and years before 1582 as per the requirements.
Congratulations! You have successfully implemented the leap_year function and tested it with different input values.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



