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 trial
Andrew Morgan
577 PointsWhy is this failing?
I was doing the first even_odd challenge:
Write a function named even_odd that takes a single argument, a number. return True if the number is even, or False if the number is odd. You can use % 2 to find out the remainder when dividing a number by 2. Even numbers won't have a remainder...
I eventually got it to work, but this was my first attempt and it failed, but I don't understand why:
def even_odd(2):
if even_odd % 2 == 0:
return True
else:
return False
Is passing an integer as an argument what's causing it to fail?
Or is it because I call the function name "even_odd" in the IF statement?
I eventually got it working like this (although I reckon this is a little long-winded and could be simplified:
number = 10
def even_odd(number):
if number % 2 == 0:
return True
else:
return False
2 Answers
Steve Hunter
57,712 PointsHi Andrew,
Your first method failed because it hard-coded the 2, rather than allowing any value to be sent in. And, yes, using the method from inside itself would cause problems too!
The second method is good but, yes, it can be simplified.
Consider what number % 2 == 0 evaluates to. The maths bit will either evaluate to zero or not-zero. Therefore, the result of comparing those two outcomes to zero will evaluate to true or false. So, we don't need the if statement at all. The expression can only evaluate to true or false, so there's no need to translate it into those two values - just return the result of the expression!:
def even_odd(number):
return number % 2 == 0
I hope that makes sense.
Steve.
Jennifer Nordell
Treehouse TeacherHi there! The problem lies in the understanding of arguments and parameters and the difference. A parameter is really just a variable declaration that's assigned to that function and only lives as long as the function is executing. An argument is what is sent to the function.at the call site. Your second one passed because Treehouse called your function and sent in an argument to it. A parameter in a function must be a valid Python variable name.
I've used this example before, but let's talk a bit about the purpose of a function. Imagine that you're a big business executive and you have just hired a brand new assistant. Most days, you like to get your lunch from a deli down the street. You're now going to have your assistant do this. On the first day, you give them the directions to the deli, you tell them what you want to eat and drink, explain how to charge it to the company, and then tell them to return with your food.
Ideally, you only want to explain how to get to the deli and charge it to the company once. If you had to do that every day for the same assistant, you'd probably fire them. So what we want is a function go_to_deli and everytime we tell the assistant to go_to_deli they go (without repeating instructions) and pick up our lunch.
The problem right now with our go_to_deli function is that we have no way to change our order. We need it to have parameters to accept what we want to eat and drink. We might not want to have the same thing every day. We could do something like this:
def go_to_deli(food, drink):
print("I have your {} and {}, boss.".format(food, drink))
go_to_deli("caesar salad", "iced tea")
go_to_deli("barbeque chicken bagel", "Coca-cola")
This allows us to pass our food and drinks as arguments to the go_to_deli function. Whatever is first in the parentheses at the call site will be assigned to the food variable in the function, whatever is second will be assigned to the drink variable.
Hope this makes sense, but let me know if you have any questions about that.
And yes, you're correct, the code for the challenge could be shortened, but it's a little over the level of a "Basics" course in my opinion. You could have written passing code in two lines:
def even_odd(number):
return number % 2 == 0 #this returns the resulting boolean value directly
Hope this helps!
Andrew Morgan
577 PointsMassive help, thank you so much!
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherSorry Steve, I had a phone call while I was composing my answer. I should have refreshed the page first
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsReally not a problem, Jennifer!

Andrew Morgan
577 PointsAndrew Morgan
577 PointsThat's great! I think the confusing part of your answer is that the challenge specifies:
But I guess in your solution, it would only return a value if the number was even (thereby returning True). Similarly, if the number was not even, it would return no value (i.e. False).
Do you reckon that's a valid understanding of your solution?
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsHi Andrew,
Yes, I think you're spot on.
The expression,
num % 2 == 0will evaluate toTrueifnumholds a value of 2, 4, 6, etc. because the result ofnum % 2does equal zero, so you are making a comparison of0 == 0. While, ifnumcontains an odd number,num % 2does not evaluate to zero (it'll be 1) and1 == 0isFalse. So, theifstatement becomes redundant; we can just return the result of the comparison.Make sense?
Steve.