Is_even


#1

Please help the instructions told me to quote "1.

Define a function is_even that will take a number x as input.

If x is even, then return True.

Otherwise, return False."

When i looked at my code it looked exactly what the instructions wanted so i clicked run. But it failed because there is something wrong with the else and it is not a problem involving indents.

Please show me the answer.

This is what i have so far.

def is_even(x):
if x is even:
return True
else:
return False


#2

What is even within the scope of the function is_even()? It doesn’t exist.

We use modulo operator % to check the remainder of the number when divisible by some value.

Take a look at the instructions and the hint. You want to use the modulus to check if the number is even.


#3

Nice to see someone who responded, But still having a tough time with the code. Can you send me the answer please.


#4

Unfortunately, that would be against the community guidelines.


#5

Well how did you fix the code. What made your code different from mine. This is what i have so far.
if x is even:.


#6

Well you can’t just say if x is even because, as mentioned by @hellofromtonya, even isn’t defined. You need to find another way of checking for this. Try thinking of what an even number is and use that logic in your program.

Try re-reading what we have told you already…


#7

As @aquaphoenix17 told you, we can’t just give you the answer, as that’s cheating. But we can nudge you in the right direction.

Step 1 - What makes a number “even” or “odd”?

What is an even number? What makes it even?

2, 10, 50, 80, and so on are all even numbers. Right?
3, 7, 27, 149, etc. are odd numbers.

So if 10 is even and 7 is not, what makes 10 even?

Hint: Think of your maths and how we figure out if a number is even or not.

Go ahead and answer that question. And then we can work through the rest of it together.


#8

The difference between an even number and an odd number is that an even number is divisible by 2 while an odd number is not. Also how can i use the modules % to check my code. I saw not examples of it before.
Anyway thank you for all of the help. It really means a lot to me.


#9

Exactly. Even numbers are evenly divisible by 2.

10 / 2 = 5 with 0 remainder

A modulo returns the remainder to you. So if you did 10 % 2 == ? what would the result of that be? What would it equal?

Then take that equation and use it as your checker to determine if a number is even. If yes, that conditional expression returns True. But if no, then it returns False.


#10

10 divided by 2 is 5.
So i first put def is_even(x):, then 10 % 2 == 5, then say return True, then say else:, than say return False.
Also how do i make else: a valid syntax, Code Academy keeps telling me that during difficult lessons but i could never find it in the glossary. Anyway Thanks i think i am picking up the scent.


#12

Let’s put together what we know.

  1. An even number is evenly divisible by 2. It has no remainder.
  2. An odd number is not evenly divisible by 2. It will have a remainder.
  3. In code, we can get the remainder of division by using the modulo operator `%.

The modulo operator % gives you the remainder. For example,

  • 7 % 2 = 1 meaning that when you divide 7 by 2 you get a remainder of 1. Right?

  • 10 % 2 = 0 meaning that when you divide 10 by 2 it goes in evenly with 0 as it’s remainder.

7 / 2 = 3 with a remainder of 1. Right? Therefore, using modulo, we represent that as 7 % 2 = 1.

Time to make it Code

  • let x be the number we are evaluated.
  • if x % 2 is even, then the remainder is 0; else it’s an odd number.
  • in code we compare equality using a double equal sign ==

Therefore, we can say if x % 2 == 0 then it’s an even number. Else, it’s odd.

Now finish your function.


#13

modulus is the divisor. modulo is the remainder.

Using a calculator in math class should be a crime, but it is common practice these days.

17 / 5

in a calculator will be 3.4

Take than 0.4 decimal fraction and multiply it (on paper) by 5 (the divisor) and you get 2.0. Make it an integer and it is, 2 which just happens to be the remainder.

Below,

D = 17
d = 5
q = 3
R = 2

The latter two are computed using long division…

   ______
5  ) 17 | 3  // quotient
    -15 |
    ----
      2      // Remainder
D - R = q * d    ~ plus R - q * d to both sides

gives,

D - q * d = R

but,

D - q * d = D % d

therefore,

R = D % d

#14

So i should set up my code just like a division problem.


#15

Nope. Use the modulo operator and get the remainder to check if it’s == to 0. If yes, then it’s even.

x % 2 == 0.


#16

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