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 trialKong Cheong
633 PointsPython basics: number-game-app/even-or-odd. I am a little stuck on how to write this one.
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....
4 Answers
Kevin Faust
15,353 Pointsfirst create the function. a number will be passed in as an argument:
def even_odd(number):
check if its even using the % sign. if we divide the number by 2 and we get 0, then it is even. return true if this is the case.
def even_odd(number):
if number % 2 == 0 :
return True
else:
return False
or a short form
def even_odd(number):
return number % 2 == 0
which directly returns whether or not the remainder after the division is 0
Kong Cheong
633 PointsCool! Thanks Kevin. I was testing around and I was very close while trying to get help. thanks again!
Kevin Faust
15,353 Pointsglad ya understand now
Jonathan Newsome
2,773 PointsI know this is a year old, but here is what I tried and it worked fine:
def even_odd(number): if not number % 2 : return True else: return False
Bryan Castillo
2,484 Pointsthe % part confused me big time. anyone care to explain?
Marc Dantin
965 Points% is different from "/" which indicates division. What % returns is the remainder of a division. 12 / 3 = 4 12 % 3 = 0
or
5 % 3 = 2 (5 divided by 3 gives you 1 with a remainder of 2, the "%" only returns that "2")