-2
def is_leap_year(year):
    if (year%4==0 & (year%100!=0)) | (year%4==0 & year%100==0 & year%400==0):
        return True
    else:
        return False
print(is_leap_year(1900))

I would like for the argument (1900) to yield a False, since both sides of the or ( | ) statement fails to be true. What am I missing? This is written in Python.

Labbsserts
  • 99
  • 2
  • 1
    The boolean and/or operator in Python are the words `and` and `or`, not the ampersand and vertical bar. Those are bitwise operators. – Tim Roberts Nov 10 '22 at 22:08
  • Don't use `&` and `|` when you mean `and` and `or`; they don't short-circuit. and their precedence is higher than the comparison operators. – chepner Nov 10 '22 at 22:09
  • `a == b & c == d` is the same as `a == (b & c) == d`, not `(a == b) & (c == d)`. – chepner Nov 10 '22 at 22:10
  • 1
    You also don't need an `if` statement; the Boolean you use in the condition is the value you want to return, so just return it directly. – chepner Nov 10 '22 at 22:11
  • Thanks everybody for your input. @chepner yes, I can see that now, thanks for the observation. – Labbsserts Nov 10 '22 at 22:19

1 Answers1

1

Try

def is_leap_year(year):
    if (year%4==0 and year%100!=0) or (year%4==0 and year%100==0 and year%400==0):
        return True
    else:
        return False
print(is_leap_year(1900))

& and | are bitwise AND and OR respectively, not logical operators.

tkburis
  • 101
  • 1
  • 13