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

Luis Aguilar
Luis Aguilar
750 Points

I don't know where to go after making the function 'even_odd'

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....

2 Answers

Christophe Pouliot
Christophe Pouliot
6,495 Points

If you want to make a short one you can do

def even_odd(num):
    return False if num % 2 else True

If the number is not a multiple of 2 the value of num % 2 will be 0, which is "Falsy".

Otherwise, return True if the value of num % 2 is not 0 (Which is "Truthy").

What you could also do is

def even_odd(num):
    return not bool(num % 2)

This casts the value of num % 2 to a boolean, which will be False if the value is 0 and True if the value is other than 0 (Which means there is a remainder). You then take the opposite (the not) because the answer will be True if there is a remainder, and you want the answer to be True if the number is even (no remainder).

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

I can't see your code but you can solve it like this:

def even_odd(num_arg):
    if num_arg % 2 == 0:
        return True
    else:
        return False

I hope that helps a little bit