Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Number Game App Even or Odd

Russell Berry
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Berry
Python Web Development Techdegree Student 1,640 Points

My bad code passes this code challenge.

This code passes the challenge, but according to my... brain math... it shouldn't work!

Think about it. If int == 11, then it'll return 5.5... which is not zero?

even.py
def even_odd(int):
    if (int % 2) == 0:
        return(True)
    else:
        return(False)

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Russell,

Your code is not 'bad.' It is returning exactly what needs to be returned. I do think, however, you are misunderstanding the modulo operator. It returns the remainder of the division operation, not the actual division answer. So, if the INT is 11, the modulo is 1. (11 divided by 2 is 5 remainder 1). So, in this case, your code will return false (because it doesn't equal zero and is, therefore, not even and will execute the else clause). If you use 12, it will return true (12 divided by 2 is 6 with 0 remainder which does equal 0).

Does that make sense? :dizzy:

Russell Berry
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Berry
Python Web Development Techdegree Student 1,640 Points

Wouldn't any number you put in for INT higher than zero return false regardless of whether it has a remainder or not? I can't seem to remember how to make it look for a float versus a whole number.

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Your conditional is checking to see if the modulo of the int is zero, and the modulo doesn't return a float, it only returns an int.

Example:

  1. 15 % 4 ==> Returns 3 (4 -- 8 -- 12 and 3 left over)
  2. 15 % 3 ==> Returns 0 (3 -- 6 -- 9 -- 12 -- 15 and none left over)

This function is checking even/odd numbers, which is why you are using %2. If an int has no remainder when divided by 2 then it's even. If it has any remainder, then it is odd.

So,

  1. 15 % 2 ==> Returns 1 (thus it returns false on the if condition and executes the odd.
  2. 14 % 2 ==> Returns 0 (thus it returns true on the if condition and executes that.
Russell Berry
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Berry
Python Web Development Techdegree Student 1,640 Points

Ok I'm still not understanding something so bear with me.

so this line: if (int % 2) == 0: is not checking to see if my integer divided by two is zero, but rather it is determining if the int divided by 2's remainder is zero? would this code: if (int % 2) = 0: be checking to see if my int itself became zero after being divided by 2?

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Correct.

If you were going to check the division it would be if (int / 2) == 0 So, if the `int passed in was 15, with division, the answer to 15 / 2 would be 7.5, but the modulo 15 % 2 would be 1 (7 remainder 1).

Here's a good Resource for all operators.

Russell Berry
seal-mask
.a{fill-rule:evenodd;}techdegree
Russell Berry
Python Web Development Techdegree Student 1,640 Points

Jason you are some kind of code magician. For some reason I thought I heard way early on in the beginning of the python videos that % was used for division, and I thought "Well... that's pretty wierd but ok!"

Thanks for clearing that up for me! That link is really great!!!