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 trialNicolas Cortinas
691 Pointseven odd task
Hello guys!
I would like to know why this function dont work, I know the correct solution (return not % 2) I just want to know why this attempt didnt work.
Thank you very much!
Nico
def even_odd(num):
if num / 2 == int:
return True
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! First, you're using the division sign instead of the modulus operator. Division gives us the result of dividing two numbers. So if I had sent in the number 26 to your function, the result of dividing by 2 would be 13. But what we want is the remainder of dividing by 2. And we do that with the modulus operator %
. If we did that and I sent in 26, then the result would be 0. Twenty-six divided by 2 results in thirteen with a remainder of 0, which is what the modulus operator returns. The result of 26 % 2
would be 0. The result of 27 % 2
would be 1.
Then we'd need to compare the result of the modulus against 0... not int. If the modulus resulted in 0 then the number is evenly divisible by that number. And, of course, even numbers are evenly divisible by 2. Odd numbers are not.
Hope this helps clarify things!
Nicolas Cortinas
691 PointsNicolas Cortinas
691 PointsHello, Jennifer! Yes! It really helped me out! Thank you very much! I appreciate the time you took to answer me! Thanks!