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 Practice Creating and Using Functions in Python Practice Functions That's Odd

Justin Armstrong
Justin Armstrong
3,702 Points

Python function

I don't understand what I am doing wrong

def is_odd(number):
    if not number / 2:
        return True

2 Answers

I wrote it out with a decision structure that looked like

# pseudocode:
if myNumber is even:
  return False
  return True

Maybe I'm finicky but I like to be able to explicitly return either True OR False, so I like to get that else statement in there.

That could just be me being inelegant, though.

Your test should involve the modulo operator. The test for even or odd qualities of a number comes down to the remnant after you do division by 2, (i.e. it comes down to NUMBER modulosymbol TWO...does that produce zero or not?)

Robin Goyal
Robin Goyal
4,582 Points

So if your function is checking if a number is odd, you are just missing a few things but very close!

Right now, the line not number / 2 will evaluate to true or false depending on the value of number. Recall the truthyness and falsyness of values. For numbers, 0 evaluates to False and anything that is not 0 is True. Therefore, if number is equal to 6, then 6/2 is 3 and not 3 would therefore be True and if your function would then return True even though 6 is an even number.

The modulus operator (%) gives the remainder for an operation 10 % 3 returns 1. Try to use modulo to determine if a number is a remainder or not.

Also, your function is returning True but also remember to return False in case a number is not odd. I hope this guides you to the right solution!