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 trialChris H
Python Web Development Techdegree Student 1,368 Pointseven_odd challenge
Can someone please help with this code challenge not sure what i am missing?
Thanks!
def even_odd(num):
if num even:
returne (True)
else:
returne (False)
1 Answer
hamsternation
26,616 PointsYou're missing a few things here, first, let's break down your if logical statement.
def even_odd(num):
if num even:
returne (True)
for logical statements to work, there has to be something that tells the logical statement what it's comparing against, and if the answer is "True" or "truthy" it goes on to process whatever is indented.
Let's think about your if statement for a second. The program knows it's doing something with the num variable because you typed if num
, but the even part isn't something python would understand naturally. Basically, python thinks it's just another variable, and not a logical statement.
A logical statement a lot of times can come with comparison, like greater than, less than, or equal to.
if num == even:
would be a valid logical statement, but the problem is, python still doesn't know what "even" is.
I would consider refreshing yourself on the modulo (%), think of it as a division operator or addition, but it finds the remainder of 2 numbers instead.
So the logical statement:
if num % 2 = 0:
Would be equivalent to saying, num divided by 2 yields a remainder of 0, which is exactly what "even" means, but in a way python actually understands. See, you always have to translate what you're trying to do to python.
The following logical statement would see if the modulo of num is 0, if it is, return true, if it's not, return false.
def even_odd(num):
if num % 2 == 0:
return True
else:
return False
You also made an error in typing return. Python is very specific in both the capitalization and the spelling of its keywords. An easy way to see if you typed a keyword right is if it turns a different color in the treehouse console, which you will see when you type the word 'return' as opposed to 'returne.'
Finally, the return statement also doesn't require parenthesis, as it is not a function. This may be a bit confusing at first, but it's not the same as a print() statement. Think of return
being closer to the def
or if
statements, no parenthesis needed.
I hope this helps and good luck on your python endeavors.
Chris H
Python Web Development Techdegree Student 1,368 PointsChris H
Python Web Development Techdegree Student 1,368 Pointsthank you for breaking it down for me i will also take your advice and review (%).