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

Hi,I tried looking for help online but Im yet to come across any websites the can help me more understand this function

create_a_function.py
def is_odd num:
    if num % 2 != 0:
        returns True
        else returns False
Jeremy Charles
Jeremy Charles
Courses Plus Student 643 Points

Hi The % sign is known as the modulo operator. The modulo operator returns the remainder of a division operation. example: 2 goes into 4 two times, leaving no remainder however 2 goes into 3 once leaving a remainder of 1 The remainder: 1, that was left over from the example above is what the % (modulo operator) returns.

So we can see now that (any number % 2) that returns a remainder greater than 0 is an odd number.

Further examples: 8 % 2 == 0 (even) 9 % 2 == 1 (odd) 512 % 2 == 0 (even) 777 % 2 == 1 (odd)

1 Answer

'else' indent is not right in your pasted code. Should be like this:

def is_odd num: if num % 2 != 0: returns True else returns False

also to explain %

5 % 2 = 1 .... whereas 1 is the remainder of 5 divided by 2 6 % 2 =0 .... 6 is divisible by 2 with a remainder of 0.

therefore the function above tests a num to see if it returns 0 or something else.

I hope this helps.