Python Forum

Full Version: Leap Year Issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I am facing an issue when I use Years' like 100, 1000. I am not sure, why is this happening.

print("***********Leap Year**************")
Year = int(input("Please enter an year to see if it is a Lear Year "))
print("Year entered is ", Year)
while Year!=0:
    if(Year % 4 == 0):
        if(Year % 100 !=0 or Year % 400 == 0):
            print(f'{Year} is a Leap Year')
            Year = int(input("Please enter an year to see if it is a Lear Year "))
            print(Year)
    else:
        print("Not a Leap Year")
        Year = int(input("Please enter an year to see if it is a Lear Year "))
        print(Year)
Output:
Please enter an year to see if it is a Lear Year 400 Year entered is 400 400 is a Leap Year Please enter an year to see if it is a Lear Year 100 100 Process finished with exit code -1
Interesting fact about leap years.
1 day every 4 years is a bit too much correction,
thus any year divisible by 4 is a leap year -- if --
the century is also divisible by 4 thus:
2000 was a leap year, but 1700, 1800, qnd 1900 were not.

Actual number of days in a year: 365.2422
Actually, your code is right
When it comes to years like 1000, 2000, 100, 500 it is divided by 400, not 4 and if there is no remainder, it isn't a leap year
At line 6 you check to see if the year matches a leap year. But what happens if it doesn't? Since there's no else clause for that if, it just falls past the block to the end of the while.

The while loop runs again, but without changing the year. So it just tests over and over again.