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

How would I create a function that determines if a specific value is odd?

I have written this so far def is_odd(integer): return "2" + integer + "5"

1 Answer

Hi Reagan,

As your function is called "is_odd" you should probably return a boolean (true or false).

Your current function will fail with a type error if passed an integer, and if passed a string will concatenate it, prepending 2 and appending 5.

How do we know if a number is even or odd? We need a rule for one or the other so that we can seperate our numbers into those that meet the rule and those that don't.

All even numbers are divisible by 2 with no remainder, and no odd numbers are, so that works. We can use the modulo operator to check if the remainder after division is 0.

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

Hope that helps,

Eric