Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Leap Year Issue
#1
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
Reply
#2
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
Reply
#3
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
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to get year not the entire year & time mbrown009 2 862 Jan-09-2023, 01:46 PM
Last Post: snippsat
  Problem with module time and leap seconds Pedroski55 3 1,190 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
Photo Algorithm for leap year (structogramm coder_sw99 3 2,194 Jul-23-2021, 02:13 PM
Last Post: DeaD_EyE
  Leap Year MartinEvtimov 14 24,990 Mar-10-2017, 01:46 AM
Last Post: Larz60+
  leap year program issue jashajmera 3 3,824 Feb-04-2017, 11:51 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020