Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Python] Leap year finder
03-14-2010, 06:52 AM
Post: #1
[Python] Leap year finder
From: "http://www.openbookproject.net/pybiblio/practice/wilson/"

Description
Leap years occur according to the following formula: a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1993 and 1900 are not. The next leap year that falls on a century will be 2400.

Input
Your program should take a year as input.

Output
Your program should print whether the year is or is not a leap year.

Sample Session
What year: 1999
1999 is not a leap year.

What year: 1988
1988 is a leap year.
Code:
y=int(raw_input("What year: "))
if y%400==0:
    print str(y)+" is a leap year."
elif y%100==0:
    print str(y)+" is a leap year."
elif y%4==0:
    print str(y)+" is a leap year."
else:
    print str(y)+" is not a leap year."
This is a trivial task. But I also like doing things like this. Putting it in a loop so user can input as many times as he likes without having to re-run the program.
Code:
while (True):
    y=int(raw_input("What year(-1 to stop): "))
    if y==-1:
        break
    elif y%400==0:
        print str(y)+" is a leap year."
    elif y%100==0:
        print str(y)+" is a leap year."
    elif y%4==0:
        print str(y)+" is a leap year."
    else:
        print str(y)+" is not a leap year."
I will do those easy tasks and post them. Coming soon. Big Grin

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
03-14-2010, 07:02 AM
Post: #2
RE: [Python] Leap year finder
Very good task. I was dong this leap year tasks, and they are interesting.
Also one description for leap years (at least last 20) is that Leap year is the one in which Olympic Games were held LOL

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
03-14-2010, 02:29 PM
Post: #3
RE: [Python] Leap year finder
yeah I was also doing those tasks using C++.
nice source btw.
Visit this user's website Find all posts by this user
Quote this message in a reply
03-14-2010, 02:57 PM
Post: #4
RE: [Python] Leap year finder
You can use the built in function also.

Code:
import calendar;
year = int(raw_input("Enter a year"));
print(calendar.isleap(year));

I don't have a compiler on this computer because im not on my system, but if I remember correctly it goes something like that Smile
Visit this user's website Find all posts by this user
Quote this message in a reply
03-15-2010, 01:08 AM
Post: #5
RE: [Python] Leap year finder
Somehow I completely forgot about C++. Didn't work in it for months. Well, not that I'm good in it, but it would be nice if I do some tasks in c++ from time to time.

I don't know many built in functions. Maybe I should dedicate some more time to find some interesting functions. Smile

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
05-14-2010, 10:14 AM (This post was last modified: 05-14-2010 10:17 AM by Rich69.)
Post: #6
RE: [Python] Leap year finder
(03-14-2010 06:52 AM)Alphablend Wrote:  From: "http://www.openbookproject.net/pybiblio/practice/wilson/"

Description
Leap years occur according to the following formula: a leap year is divisible by four, but not by one hundred, unless it is divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1993 and 1900 are not. The next leap year that falls on a century will be 2400.

Input
Your program should take a year as input.

Output
Your program should print whether the year is or is not a leap year.

Sample Session
What year: 1999
1999 is not a leap year.

What year: 1988
1988 is a leap year.
Code:
y=int(raw_input("What year: "))
if y%400==0:
    print str(y)+" is a leap year."
elif y%100==0:
    print str(y)+" is a leap year."
elif y%4==0:
    print str(y)+" is a leap year."
else:
    print str(y)+" is not a leap year."

Hey dude, not to take the wind out of your sails or anything but i noticed what i think is a problem with your code. A leap year as said is a year that can be divided by 4 but not 100 UNLESS it can be divided by 400 right? (I'll look such an idiot if i've got this wrong).

Anyway the year 1000 is NOT leap year based on these rules, it can't be divided by 400 but can be divided by 100. However if you run your program and enter 1000 is spits out that it is a leap year. Now this isn't an actual forumla issue here either because you have the formula's corret, i think. It might just be a typo on the

Code:
elif y%100==0:
    print str(y)+" is a leap year."

line

I'm doing the same task myself as part of my University course work lol. I got:

Code:
def LeapYear(year):
    if year % 400 == 0:
        return True
    elif year % 100 == 0:
        return False
    elif year % 4 == 0:
        return True
    else:
        return False

print (LeapYear(400))
print (LeapYear(1000))
print (LeapYear(1012))
print (LeapYear(2000))
print (LeapYear(2010))

A very raw and basic output i know but i just care about getting my head around the formula's at the moment.

Rich

P.S Sorry about posting in a 2 month old thread. My bad
Find all posts by this user
Quote this message in a reply
05-14-2010, 12:56 PM
Post: #7
RE: [Python] Leap year finder
Ah thanks for the reply Rich. Reviving threads really isn't a bad thing here...If you find a problem with the thread, it's better to point it out, to be corrected. Then to leave this thread with incorrect information.

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
05-15-2010, 01:29 AM
Post: #8
RE: [Python] Leap year finder
I think that you are right Rich69.
Leap year can be divided with 4. If you can divide it with 100 than you need to divide it with 400 to be sure it is leap.
1900 is not a leap year (but it can be divided with 100 but not with 400)
2000 is a leap year (it but it can be divided with 100 and 400)

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
11-02-2010, 05:52 AM (This post was last modified: 11-02-2010 05:53 AM by Alphablend.)
Post: #9
RE: [Python] Leap year finder
Wow, yeah. My bad. Thanks for correcting my code. Cool

[Image: 45669_pythonlogo.png][Image: 45668_javalogo.png]
Find all posts by this user
Quote this message in a reply
11-02-2010, 10:40 PM (This post was last modified: 01-21-2011 07:37 PM by codecaine.)
Post: #10
RE: [Python] Leap year finder
Since python is a script language you can view all it's source code. Here is how they did their function. This code is located in calendar.py

Code:
def isleap(year):
    """Return 1 for leap years, 0 for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: