Issue
I'm trying to figure out if a year is a leap year or not I wrote this code, but I don't understand why it work, when I input 1996 it's "true" and when I enter 1700 it's "false" but why doesn't the "and" function returns false on 1996? shouldn't it check the left side and return a "false" ? ps. I work with anaconda3.2020-02:
bool(((x%100 == 0) * x%400 == 0) and x%4 == 0 )
Solution
I'm not sure why you are using *
at all. The expression you want is
x % 400 == 0 or (x % 100 != 0 and x % 4 == 0)
(bool
is a subclass of int
and True == 1
, so and
is sort of like and
, but True * True
is 1
, not True
, and True + True
is 2
, not True
or even 1
. Don't use arithmetic with Boolean values.)
Answered By - chepner
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.