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

Struggling with creating and using functions python code challenge

The instructions are: Let's create a function that determines if a specific value is odd. Let's name the function is_odd and have it declare a single parameter. It should return True if the value is not divisible by 2. This is my code so far:

def is_odd(number):
    if number % 2 == 0

I can't figure out what the return should read. I've looked at the hints but they aren't clicking. Any help?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Lauren` Welch, without giving he whole answer away, you can structure true/false return values i two ways:

  1. Using an if statement (like you have done), decide which value needs to be returned, the use either:
    return True
# or 
    return False
  1. If you can conveniently state the true/false decision in a single phrase, you can return the result redirectly:
# if you want true for a phrase that evaluates true use
    return <statement_evaluates_true>
# if you want false for a phrase that evaluates true use
    return not <statement_evaluates_true>

So say you want True if age grater than 21, use either:

def old_enough():
    if age > 21:
        return True
# or, use
def old_enough():
    return age > 21

Post back if you have more questions good luck

Thank you Chris! I changed my code from

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

When running 1 through the code it was returning false so I changed it to this

def is_odd(number):
    if number % 2 != 0:
        return True