samedi 3 septembre 2016

Python if True *unless*

I am trying to figure out a way to return true if input is divisible by 4, but not 100 unless also by 400. I've unsuccessfully tried:

def is_leap(year):
    leap = False

    if [year % 100 == 0 if not year % 400]:
        leap = False
    else:
        if leap % 4 == 0:
            leap = True
    return leap

input of 2100 is divisible by 4 and 100 but not 400, so I want it to return False.

I've come to a simple conclusion of

def is_leap(year):
    leap = False

    if year % 4 == 0 and year % 400 == 0:
        leap = True
    elif year % 4 == 0 and year % 100 != 0:
        leap = True
    return leap

But is there an easier or different way to do an unless? Thank you.

Also, while I am primarily leaning towards python3.5 for what everyone's doing, I enjoy the simplicity of 2.7. Any help in either would be appreciated.

Aucun commentaire:

Enregistrer un commentaire