need some help on the coding can some plz help
the question is:
A year is a leap year if it is divisible by 4, unless the year is a Century, in which case it is
only a leap year if it is divisible by 400. Write a function which takes a year as an argument
and prints a message to the screen saying whether or not that year is a leap year.

this the ouline that i have got:

1 def isLeapYear ( year ):
2 ...
3
4 # Testing ...
5 isLeapYear (400)
6 isLeapYear (800)
7 isLeapYear (1600)
8 isLeapYear (1601)
9 isLeapYear (2004)
10 ...

ive come up with some coding of my own but it doesnt work:

((Year % 4 == 0) and (not (Year % 100 == 0))) or (Year % 400 == 0)
(Year % 4 == 0) and not (Year % 100 == 0) or (Year % 400 == 0)

Recommended Answers

All 18 Replies

Try this

# python >= 2.5
def isLeapYear(year):
    return False if year % 4 else True if year % 100 else not year % 400

:)

first of all thanks for helping now i have try the code but it comes back with

indentationerror: expected an indented block

You probably made a mistake when you copied the text. Note that the line containing return starts with 4 spaces.

ok i have added the 4 spaces and no errors displayed but three dots come

...

so shall i add the rest of the code provide or was it going to print it all automatically

ok i have added the 4 spaces and no errors displayed but three dots come

...

so shall i add the rest of the code provide or was it going to print it all automatically

Three dots? Are you using the Python shell?

how do i make the code print out this info automaically

4 # Testing ...
5 isLeapYear (400)
6 isLeapYear (800)
7 isLeapYear (1600)
8 isLeapYear (1601)
9 isLeapYear (2004)
10 ...

Something like this:

def isLeapYear(year):
    """
    return True if year is a leapyear
    """
    return (not year%4 and year%100 or not year%400) and True

# Testing ...
print( isLeapYear(400) )
print( isLeapYear(800) )
print( isLeapYear(1600) )
print( isLeapYear(1601) )
print( isLeapYear(2004) )

on line 2 ... when i press enter an error comes up so how can i enter the rest of the code

hey thanks sneekula for the help and thanks to everyone who help it worked thanks alot

I suggest that you put sneekula's code in a file named 'leapyear.py' and then you run the file.

i guess this should do the trick...

int leap ( int int x)
{
                       if  (!(x%100) && ( x %400 || x %4))
                                     return 1;
 }

well it will return 1 if the year is leap year or not

i guess this should do the trick...

int leap ( int int x)
{
                       if  (!(x%100) && ( x %400 || x %4))
                                     return 1;
 }

well it will return 1 if the year is leap year or not

Ouch, you are in a different world! That looks like old, ugly, and badly written C code.

well at first i completyl apologise cuz when i saw the thread , i actually didnt navigate thru the forum section , so just mistook as c form

second of all

Ouch, you are in a different world! That looks like old, ugly, and badly written C code.

well even though its a c code , i dont see any kind of ugliness in the code.i am glad if u could be specific in pointing out the so called
"old, ugly, and badly" in the code.

ha ha u guys r funny

how would you do this if you are using python 3.2?

how could i find leap year , using python programing also by using constructors................

how could i find leap year , using python programing also by using constructors................

I think this old thread contains the answer of the leap year problem. Did you test the solution ? Otherwise you can start a new thread and post your code.

The c code line
int leap ( int int x)
will give error: two or more data types in declaration of `x'
this is bad coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.