Leap Year or Common Year

PythonPythonBeginner
Practice Now

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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ErrorandExceptionHandlingGroup -.-> python/catching_exceptions("`Catching Exceptions`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302741{{"`Leap Year or Common Year`"}} python/numeric_types -.-> lab-302741{{"`Leap Year or Common Year`"}} python/type_conversion -.-> lab-302741{{"`Leap Year or Common Year`"}} python/conditional_statements -.-> lab-302741{{"`Leap Year or Common Year`"}} python/function_definition -.-> lab-302741{{"`Leap Year or Common Year`"}} python/catching_exceptions -.-> lab-302741{{"`Leap Year or Common Year`"}} python/python_shell -.-> lab-302741{{"`Leap Year or Common Year`"}} python/build_in_functions -.-> lab-302741{{"`Leap Year or Common Year`"}} end

Implement the leap_year Function

In this step, you will learn how to implement the leap_year function in the leap_year.py file.

  1. Open the leap_year.py file in your preferred code editor.
  2. Inside the leap_year function, 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".

  1. Save the leap_year.py file.

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.

  1. Open a terminal or command prompt and navigate to the directory where the leap_year.py file is located.
  2. Run the leap_year function by executing the following command:
python3 leap_year.py
  1. 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.

  2. 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.

Other Python Tutorials you may like