Python Calendar Leap Year Function Code Not Working

40
January 28, 2019, at 9:30 PM

So I am currently trying to create a function whereby when you input a specific year and calendar.isleap(year) will then process whether that year is a leap year with True or False.

My issue is I can't seem to figure out a function whereby it will decide whether the year input is True of False and will then print out that year along with a string. The example I have created and tried to use is below:

import calendar
def leap_year():
    year = calendar.isleap(year)
    if calendar.isleap == True:
        print(year + 'is a leap year')
    else:
        print(year + 'is not a leap year')
year = 2016
leap_year()

Any help on this would be appreciated. Thanks

Answer 1

The isleap() method returns True if the year is a leap year, otherwise False.

import calendar
print(calendar.isleap(2016))
Answer 2

A couple of changes, you need to pass in the year to the function and have "year" as a variable in your function, and then cast the year to a string when printing.

import calendar
def leap_year(year):
    if calendar.isleap(year) == True:
        print(str(year) + ' is a leap year')
    else:
        print(str(year) + ' is not a leap year')
leap_year(2016)
Answer 3

The code could have been

def leap_year(check_year):
    if ((check_year%4==0)and not(check_year%100==0))or (check_year%400==0)  :
        print(str(year) + 'is a leap year')
    else:
        print(str(year) + 'is not a leap year')
year = 2016
leap_year(year)
Answer 4

There are several problems with your code. First, your function creates a new local variable named year that shadows the global variable. Due to how name lookup works, you can't pass the global variable year to calendar.isleap and assign the result to the local variable. Use a different name for the local variable. Second, you want to see if the result of calendar.isleap is True, not the function itself. Use your new local variable in the expression.

def leap_year():
    result = calendar.isleap(year)
    if result == True:
        print(str(year) + 'is a leap year')
    else:
        print(str(year) + 'is not a leap year')

As a matter of style, don't compare a boolean variable to a boolean constant; just testing the variable directly:

if result:
    print(year + 'is a leap year')

This means that you don't even need the local variable; just use the function call directly in the if statement:

def leap_year():
    if calendar.isleap(year):
        print(year + 'is a leap year')
    else:
        print(year + 'is not a leap year')

Finally, using a global variable here is bad style. Instead, pass they year to check as an argument to leap_year.

def leap_year(y):
    if calendar.isleap(y):
        print(str(y) + 'is a leap year')
    else:
        print(str(y) + 'is not a leap year')
year = 2016
leap_year(year)  # Or simply leap_year(2016)
READ ALSO
How can plot I circles and fill the intersections that lies with the foreground image in Python?

How can plot I circles and fill the intersections that lies with the foreground image in Python?

I am a python starter and I am doing a project of masking images(single-petalled flowers) through the idea from perfect circles https://designshacknet/articles/graphics/twitters-new-logo-the-geometry-and-evolution-of-our-favorite-bird/

25
How to check if a data is a map (key-value pairs)?

How to check if a data is a map (key-value pairs)?

Before processing data (a DataSnapshot coming from Firebase), I need to check if data returned are well formated as a Map before deserializing through an Interface

36
I need to find a DES code for mobile wallet application [on hold]

I need to find a DES code for mobile wallet application [on hold]

I need a java code which provides security for a mobile wallet application and with a re sizable key sizePlease I beg someone to help me with this

42
Java 8u201: no cipher suites in common

Java 8u201: no cipher suites in common

After updating to Java 8u201 I am getting an exception when trying to connect from a SSLSocket to a SSLServerSocketThe exception states that these two clients have 'no cipher suites in common' (even on the same machine with same cipher suites enabled)

51