Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Leap Year
#11
(Mar-09-2017, 05:30 PM)wavic Wrote: So you choose the easy way? Tongue

I use calendar.isleap() only for illustration/comparison :-)
Reply
#12
(Mar-09-2017, 07:07 AM)wavic Wrote: Hello!
Put print() in each loop to see where it returns True.

For the year 2400, this will happen in the first loop because 2400 % 4 == 0 is True and will print that this year leap.

Change the conditions
def get_year():
   print("Please input year:")
   year = int(input())
   return year
 
def if_else(year):
   if year % 4 == 0 and year % 100 != 0:
     print("It's a leap year")
   elif year % 400 == 0 and year % 100 != 0:
     print("It's a leap year")
   elif (year % 100 == 0):
     print("It's not a leap year")
   else:
     print("There is an error")
 
def main():
   years = get_year()
   if_else(years)
Or put if year % 100 ==0: on top

Also, if one enter something that cannot be converted to in like text for example the script will stop with ValueError

This is working for me but why is 400 not a leap year?

(Mar-09-2017, 07:02 AM)buran Wrote: Please, wrap your code in code tags.

OK, this Larz60+ added code tags for you. Anyway, in the future always wrap your code in code tags.

2400 IS a leap year - it's exactly divisible by 400.

Quote:Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. For example, the years 1700, 1800, and 1900 were not leap years, but the years 1600 and 2000 were.


I'm apologize for the confusion I meant to say 100 not 400
Reply
#13
The algorithm states:
The year is evenly divisible by 4;
If the year can be evenly divided by 100, it is NOT a leap year, unless;
    The year is also evenly divisible by 400. Then it is a leap year. Why the year divided by 100 is not leap year.


Quote:A "truth" table that shows a number divisible by 400 and not by 4 or not by 100, hmmph. Welcome to "alternate mathematics"

It can be divisable evenly by 100 and not by 400 right? say 500


I did not simplify.
Reply
#14
(Mar-09-2017, 07:25 PM)Larz60+ Wrote: The algorithm states:
The year is evenly divisible by 4;
If the year can be evenly divided by 100, it is NOT a leap year, unless;
    The year is also evenly divisible by 400. Then it is a leap year. Why the year divided by 100 is not leap year.

Let's be accurate: it's not "the algorithm states" it is "one possible expression of the algorithm is". For reference, the leap year article on Wikipedia expresses it as:
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
and it uses a non-divisibility criterion instead of a divisibility one (and doesn't use "unless"). As a programmer, I find it hard to read because the case "divisible by 400" is really the final "else", so I find this more readable:

if (year is by 400) then (it is a leap year)
else if (year is divisible by 100) then (it is a common year)
else if (year is divisible by 4) then (it is a leap year)
else (it is a common year)
(Mar-09-2017, 07:25 PM)Larz60+ Wrote: It can be divisable evenly by 100 and not by 400 right? say 500

I'm talking about these two lines. They are about some hypothetical number that can be divided by 400 but not by 4 (the first) or by 100 (the second).

(Mar-09-2017, 02:45 PM)Larz60+ Wrote:
Output:
Truth Table ---------------------------------------------------- | Div by 4? | Div by 100? | Div by 400? | Is Leap? | ---------------------------------------------------- |     T     |      F      |      T      |    T     | ---------------------------------------------------- |     F     |      T      |      T      |    F     | ----------------------------------------------------
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#15
NIST definition -

Quote:Leap years are years with 366 days, instead of the usual 365. Leap years are necessary because the actual length of a year is about 365.242 days, not 365 days, as commonly stated. Basically, leap years occur every 4 years, and years that are evenly divisible by 4 (2004, for example) have 366 days. This extra day is added to the calendar on February 29th.

However, there is one exception to the leap year rule involving century years, such as the year 1900. Since the year is slightly less than 365.25 days long, adding an extra day every 4 years results in about 3 extra days being added over a period of 400 years. For this reason, only 1 out of every 4 century years is considered as a leap year. Century years are considered as leap years only if they are evenly divisible by 400. Therefore, 1700, 1800, 1900 were not leap years, and 2100 will not be a leap year.  However, 1600 and 2000 were leap years, because those year numbers are evenly divisible by 400.
Now I have to go back and examine what I stated earlier.

Looks like you're right again

As an non-simplified truth table mine may be correct as well
if simplified, I believe the results would be that same as yours
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,189 Oct-07-2022, 11:27 PM
Last Post: Pedroski55
Photo Algorithm for leap year (structogramm coder_sw99 3 2,193 Jul-23-2021, 02:13 PM
Last Post: DeaD_EyE
  Leap Year Issue spalisetty06 3 2,173 Jul-02-2020, 03:29 PM
Last Post: bowlofred
  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