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

create_a_function.py
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
else:
  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,578 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!