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

Jt Miller
Jt Miller
1,242 Points

Stuck on remainders

This is what it asks:

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

I start by creating the function like it asks then it says return true or false if it has a remainder so I set number to 7 an odd number so it will then I create an if statement and I am kinda confused on what to do as I know I'm not on the correct path.

even.py
def even_odd(number):
    number = 7
    if number == % 2:
        return True
    else:
        return False

even_odd(number)

1 Answer

Anibal Marquina
Anibal Marquina
9,523 Points
#Your code is almost correct, you need to remove (number=7) from the block... 
#if you want to try your code you can pass 7 as the argument print(even_odd(7)) in your local python shell.
# Also, take a look at the syntax of your if statement. 

def even_odd(number):
    if number % 2 == 0:
        return True
    return False
Christian A. Castro
Christian A. Castro
30,501 Points

Anibal Marquina Thank you so much for your advice!! However I still a bit confused with your code above. Could you be able to explain the last two returns? How did you were able to put two returns under the same section? :/ I got all lost on that part!

Anibal Marquina
Anibal Marquina
9,523 Points
def even_odd(number):
    # if statement checks if the result of divide
    # by 2 the number we provided is igual to 0
    if number % 2 == 0: 
        # if the result is 0 we return True
        return True
    else:
        # if the result is other than 0 we return false
        return False

print(even_odd(7))
# in this case the function returns False


#------------------------------------------------------------------------------------------------------------------------------------

#This code is equivalent to the one above, but is just a short version:

# The two returns are within the same function block, but the first is executed when the if statement is True (see the indentation)
# the second is out of the if statement,  is in the same level of indentation that the if statement, 
# this is understood by python as the else statement,  but you don't have to type it... 
# anyways you can also put it there to be more explicit if you want

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