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 trialRussell Berry
Python Web Development Techdegree Student 1,640 PointsMy 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?
def even_odd(int):
if (int % 2) == 0:
return(True)
else:
return(False)
1 Answer
Jason Anders
Treehouse Moderator 145,860 PointsHey 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?
Russell Berry
Python Web Development Techdegree Student 1,640 PointsRussell Berry
Python Web Development Techdegree Student 1,640 PointsWouldn'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
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsYour conditional is checking to see if the modulo of the
int
is zero, and the modulo doesn't return a float, it only returns anint
.Example:
This function is checking even/odd numbers, which is why you are using
%2
. If anint
has no remainder when divided by 2 then it's even. If it has any remainder, then it is odd.So,
if
condition and executes the odd.if
condition and executes that.Russell Berry
Python Web Development Techdegree Student 1,640 PointsRussell Berry
Python Web Development Techdegree Student 1,640 PointsOk 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
Treehouse Moderator 145,860 PointsJason Anders
Treehouse Moderator 145,860 PointsCorrect.
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
Python Web Development Techdegree Student 1,640 PointsRussell Berry
Python Web Development Techdegree Student 1,640 PointsJason 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!!!