Is_even


#1



Okay, so I feel really stupid, but for some reason my code isn't working.


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/2?curriculum_id=4f89dab3d788890003000096#


Oops, try again. is_even(19) returned True instead of the expected: False


It's supposed to check to see if a number is even.


Replace this line with your code. 
def is_even(x):
    
    if x % 2:
        return True
        
    else:
        return False


#2

This is saying, "If x modulo 2 is non-zero" which would make x odd. Switch your booleans around and it will be correct logic.


#3

if x % 2

update**
I don't see any conditionals comparator statement that is returning boolean : True/False

like, suppose I wanted to check if x is even then I'd have checked like this..
x % 2 == 0`

I hope you get the hint! :slight_smile:
Do reply if it has solved your problem !


#4

got it thanks.
Man I feel like an idiot.


#5

Keep up the good work and It happens!!! :thumbsup:
:slight_smile:


#6

Don't you?

if x % 2:
    return False

x % 2 is truthy if it is non-zero. There is nothing wrong with writing it this way. That's why I suggested switching the booleans around and leave the rest alone. It is just as valid, and also correct.

In truth, for something as trivial as testing parity, this would be the most straight forward approach:

>>> def is_even(x):
    return not x % 2

>>> is_even(42)
True
>>> is_even(33)
False
>>>

#7

Yeah, I made a mistake in observation! I've updated my answer!
:slight_smile: :thumbsup:


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.