Rafey Ahmad Rafey Ahmad - 12 days ago 9
Python Question

Leap Year Calculator

year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0 and (year % 400) == 0:
print (year, "IS a leap year.")
else:
print (year, "is NOT a leap year.")


I am using Python 3

For some reason, the program doesn't print anything after taking input. Help would be appreciated :)

Here's a link for the leap year rules if anyone needs it: https://www.wwu.edu/skywise/leapyear.html

Answer

Well, the reason that does not print anything is just as @John Gordon says: miss some else clauses in your program. I have modified to the following code:

year = int(input("Enter a year: "))
if (year % 4) == 0:
    if (year % 100) == 0 and (year % 400) != 0:
        print (year, "is NOT leap year.")
    else:
        print (year, "is a leap year.")
else:
    print (year, "is NOT a leap year.")

When a year can be divided by 4, it may be a leap year. In that sense, when it can be divided by 100 but cannot be divided by 400 ,it is not a leap year. In other situations it is. Simply you miss some cases in year % 4 == 0 situation.

For me, I usually judge leap year in following code:

def is_leap_year(year):
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        return True
    return False