Is_Even


#1



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

I don't know what I'm supposed to have as my code but I'm not good at coding :(.


Is this a GLITCH? or just me making an ERROR?


def is_even(x):
    if x % 2:
        return True
    else:
        return False


#2

Thanks is ADVANCE :smiley:


#3

when x % 2 gives 0, (when x is even)
0 is a Falsy value ,It means 0 will be seen as False

so if condition sees a "False" value and runs else part!

One thing to understand..

if (0):
   #yourcode 
else:
    #yourcode

when you give 0 as value of if condition.
0 is actually False and when if sees it as False ,It will transfer the execution to else part.
hint**

You need to switch the returning Boolean values?


#4

So I should make my code
def is_even(x):
if x % 2:
return False
else:
return True

?


#5

Weird, Why did that work?


#6

Why did it not work? (your first code)

Okay lets see..

@th3b33
when you do this..

0 == False

It will return True.
It means that 0 is equivalent/equal to False.

So in your program..when it gets an even number then x % 2 produces 0
As 0 is equivalent to False then it does not executes the if block (we know when condition turns out to be False if transfers the execution to else block)
and thus your else block executes the code and returns False.


#7

But I tried this with This code:
def is_even(x):
if x / 2:
return False
else:
return True

And it didn't work... why?


#8

Can you tell me if the last issue solved ?


#9

Last Issue? what do ya mean?


#10

This exercise?The one's code you posted?


#11

I don't get it
:frowning: :frowning:


#12

I have the code for this exercise posted up top...


#13

I was asking if your program ran and have you understood what I posted in this post?


#14

Post withdrawn :smiley:


#15

No, I have not understood... I don't get it :frowning:


#16

I'M AN IDIOT SORRY :frowning: I just figured it out... Thanks for yer help :smiley:


#17

Okay, You last code...


we're going to see why It will not work..

When you divide a number.

say if X is divided by Y,

You get two things.

X / Y -- > Quotient say Q

and

X % Y --> Remainder say R

modulo - % - is used to find this R,It means It gives you remainder not quotient.
division - / - operator gives you Quotient.

Example..

13 / 5 -- > gives you Quotient 2.
but
13 % 5 --> gives you Remainder 3.

Remainder makes sure if a number is divisible or not by some number.
so we use % operator,we can't use/ for divisibility check.


#18

What is Quotient?
:smiley:


#19

Thanks for that now I understand (other than Quotient)


#20

Haha NOT CLOSED YET.