Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Even or Odd

even_odd challenge

Can someone please help with this code challenge not sure what i am missing?


def even_odd(num):
    if num even:
        returne (True)
        returne (False)

1 Answer

26,615 Points

You're missing a few things here, first, let's break down your if logical statement.

def even_odd(num):
    if num even:
        returne (True)

for logical statements to work, there has to be something that tells the logical statement what it's comparing against, and if the answer is "True" or "truthy" it goes on to process whatever is indented.

Let's think about your if statement for a second. The program knows it's doing something with the num variable because you typed if num, but the even part isn't something python would understand naturally. Basically, python thinks it's just another variable, and not a logical statement.

A logical statement a lot of times can come with comparison, like greater than, less than, or equal to.

if num == even:

would be a valid logical statement, but the problem is, python still doesn't know what "even" is.

I would consider refreshing yourself on the modulo (%), think of it as a division operator or addition, but it finds the remainder of 2 numbers instead.

So the logical statement:

if num % 2 = 0:

Would be equivalent to saying, num divided by 2 yields a remainder of 0, which is exactly what "even" means, but in a way python actually understands. See, you always have to translate what you're trying to do to python.

The following logical statement would see if the modulo of num is 0, if it is, return true, if it's not, return false.

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

You also made an error in typing return. Python is very specific in both the capitalization and the spelling of its keywords. An easy way to see if you typed a keyword right is if it turns a different color in the treehouse console, which you will see when you type the word 'return' as opposed to 'returne.'

Finally, the return statement also doesn't require parenthesis, as it is not a function. This may be a bit confusing at first, but it's not the same as a print() statement. Think of return being closer to the def or if statements, no parenthesis needed.

I hope this helps and good luck on your python endeavors.

thank you for breaking it down for me i will also take your advice and review (%).