How do I solve this problem with leap year function?

Question:

Question:

A common year in the modern Gregorian Calendar consists of 365 days. In reality, Earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:

  1. The year must be divisible by 4

  2. If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400; therefore, both 1700 and 1800 are not leap years

Some example leap years are 1600, 1712, and 2016.

Write a program that takes in a year and determines the number of days in February for that year.

Ex: If the input is:

1712
the output is:

1712 has 29 days in February.
Ex: If the input is:

1913
the output is:

1913 has 28 days in February.
Your program must define and call the following function. The function should return the number of days in February for the input year.
def days_in_feb(user_year)

Here is my code:

def days_in_feb(user_year):
    user_year = int(input())
    return user_year

if __name__ == '__main__':
    user_year = int(input())
    if (user_year % 4 == 0) or (user_year % 100 != 0):
        print(f'{user_year} has 29 days in February.')
    else:
        print(f'{user_year} has 28 days in February.')

When I run it, I keep getting this error:

EOFError: EOF when reading a line

Please help.

Asked By: TiesTheMarsh

||

Answers:

Actually, when I copied and ran the above code, it was printing out a result, but not always a correct one as the leap year test was actually lacking in some logic.

Reviewing the requirements of the assignment, it would actually look like the testing for a leap year would be in the "days_in_feb" function. So with that in mind, following is a tweaked version of the code with the testing occurring in the function and returning an appropriate number of days for February for the entered year.

def days_in_feb(entered_year):
    days = 28                   # Default the days in February to 28
    if ((entered_year % 4 == 0) and (entered_year % 100 != 0)) or (entered_year % 400 == 0):
        days = 29               # Only update the days to 29 if the above test condition is met
    return days

if __name__ == '__main__':
    while True:
        user_year = int(input("Enter year or 9999 to quit: "))
        if user_year == 9999:
            break
        print(f'{user_year} has {days_in_feb(user_year)} days in February.')

A few things to note.

  • A while loop was added so that multiple tests could be run to check out various years.
  • In the function, the if test was updated so that February would contain 28 days by default, but would contain 29 days if the year was divisible by 4 and not divisible by 100, or if the year was divisible by 400.

Testing this out provided the following terminal output.

@Dev:~/Python_Programs/Leap$ python3 Leap.py 
Enter year or 9999 to quit: 2022
2022 has 28 days in February.
Enter year or 9999 to quit: 1900
1900 has 28 days in February.
Enter year or 9999 to quit: 1904
1904 has 29 days in February.
Enter year or 9999 to quit: 2000
2000 has 29 days in February.
Enter year or 9999 to quit: 9999
@Dev:~/Python_Programs/Leap$ 

Give that a try and see if it meets the spirit of the project.

Answered By: NoDakker

EOF error aside, your logic to decide if a year is a leap year is wrong.

The correct logic is:

# assume it is not a leap year
isleap = False

# if it is evenly divisible by 4, then it might be a leap year
if year % 4 == 0:

    # if it is evenly divisible by 400, then it is a leap year
    if year % 400 == 0:
        isleap = True

    # otherwise if it is evenly divisible by 100, then it is not a leap year
    elif year % 100 == 0:
        isleap = False

    # otherwise it is a leap year
    else:
        isleap = True

print(isleap)
Answered By: John Gordon
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.