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 trialSuleiman Abdurozikov
2,943 Pointscouldn't solve that
Help please!!!!
This one should be short and sweet.
Write a function named even_odd that takes a single argument, a number.
return True if the number is even, or False if the number is odd.
You can use % 2 to find out the remainder when dividing a number by 2. Even numbers won't have a remainder....
def even_odd(num):
??????????????
return True
else:
return False
2 Answers
Ty Thorsen
6,915 Points% is the 'modulo' operator. a % b gives you the remainder of a / b. For example:
0 / 2 = 0 with a remainder of 0
1 / 2 = 0 with a remainder of 1
2 / 2 = 1 with a remainder of 0
3 / 2 = 1 with a remainder of 1
4 / 2 = 2 with a remainder of 0.
Notice the padding emerging? Even numbers divided by 2 always result in a remainder of 0, while odd numbers always result in a remainder of 1.
So, if you want to find even numbers, you can replace the ???????? in your code like so:
if num % 2 == 0:
return True
else:
return False
Vittorio Somaschini
33,371 PointsVittorio Somaschini
33,371 PointsHello Ty.
I have changed you comment into an answer ;)