Please write a Python program that takes a user inputted year and checks whether or not it is a leap year. The program should return True if the year is a leap year and False if it is not. Additionally, the program should handle invalid inputs (e.g. non-numeric input, negative numbers) by prompting the user to enter a valid year until a valid input is given. Please use the Gregorian calendar rules for leap years (years divisible by 4 are leap years except for years that are divisible by 100 but not divisible by 400). The program should be well-documented and easily understandable for readers who may not be familiar with leap year rules or Python programming.
Please write a Python program that takes a user inputted year and checks whether or not it is a leap year. The program should return True if the year is a leap year and False if it is not. Additionally, the program should handle invalid inputs (e.g. Non-numeric input, negative numbers) by prompting the user to enter a valid year until a valid input is given. Please use the Gregorian calendar rules for leap years (years divisible by 4 are leap years except for years that are divisible by 100 but not divisible by 400). The program should be well-documented and easily understandable for readers who may not be familiar with leap year rules or Python programming. The user attention ratio is 0.560546875.
user
# Leap Year Checker

# This program takes a user inputted year and checks whether it is a leap year or not
# The Gregorian calendar rules for leap years are used (years divisible by 4 are leap years except for years that are divisible by 100 but not divisible by 400)

while True:
    try:
        # Prompt the user to enter a year
        year = int(input("Please enter a year: "))

        # Check if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400
        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
            # If the year is a leap year, print True
            print(True)
        else:
            # If the year is not a leap year, print False
            print(False)
            
        # Exit the loop if valid input has been given
        break
        
    except ValueError:
        # If the user enters a non-numeric value, prompt them to enter a valid year
        print("Please enter a valid year.")
assistant
Could you please add some comments to explain what each section of the code does? The user attention ratio is 0.2109375.
user
Sure, here's the updated code with comments:

# Leap Year Checker

# This program takes a user inputted year and checks whether it is a leap year or not
# The Gregorian calendar rules for leap years are used (years divisible by 4 are leap years except for years that are divisible by 100 but not divisible by 400)

# Loop to continuously prompt the user for input until valid input is given
while True:
    try:
        # Prompt the user to enter a year
        year = int(input("Please enter a year: "))

        # Check if the year is divisible by 4 and not divisible by 100, or if it is divisible by 400
        if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
            # If the year is a leap year, print True
            print(True)
        else:
            # If the year is not a leap year, print False
            print(False)
            
        # Exit the loop if valid input has been given
        break
        
    except ValueError:
        # If the user enters a non-numeric value, prompt them to enter a valid year
        print("Please enter a valid year.") # The program ends when a valid year is inputted and either True or False has been outputted.
assistant
Can you please add a brief explanation of how the code implements the Gregorian calendar rules for leap years? The user attention ratio is 0.271728515625.
user
Certainly! The Gregorian calendar states that a year is a leap year if it is divisible by 4 except for years that are divisible by 100. However, if a year is divisible by 400, then it is still a leap year. In Python, the modulo operator `%` can be used to determine if a year is divisible by a given number. Therefore, to implement the Gregorian calendar rules for leap years in the code, we can use the following condition:

- If the year is divisible by 4 AND not divisible by 100, OR if the year is divisible by 400, then it is a leap year. This is what the if statement in the code checks for. If the condition is true, then the program prints "True", indicating that the year is a leap year. If the condition is false, the program prints "False", indicating that it is not a leap year.
assistant