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 trialCharles Magee
Python Development Techdegree Student 657 PointsHi,I tried looking for help online but Im yet to come across any websites the can help me more understand this function
def is_odd num:
if num % 2 != 0:
returns True
else returns False
1 Answer
markmneimneh
14,132 Points'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.
Jeremy Charles
Courses Plus Student 643 PointsJeremy Charles
Courses Plus Student 643 PointsHi 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)