Reputation: 51
How come
def allEven(n):
for c in n:
if c % 2 != 0:
return False
return True
works but
def allEven(n):
for c in n:
if c % 2 == 0:
return True
return False
doesn't? With the second one, when I type allEven([8, 0, -1, 4, -6, 10]), it says it's True.
Upvotes: 0
Views: 747
Reputation: 1673
Return
Statement terminate loop and end program. If c % 2 == 0
is True, it Return True
and terminate program not check all values.
Try This
def allEven(n):
for c in n:
if c % 2 == 0:
continue
else:
return False
return True
Upvotes: 1
Reputation: 441
In your second method, you return True
once you find an even c
in n
which is not what your method supposed to do: return true IFF all c
s are even.
Upvotes: 5