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

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Shopping List Redux

Ron Chan
Ron Chan
10,987 Points

Code Challenge Task 1 of 2

In the first task of the second code challenge named Functions, I wrote the following into my text editor and ran it on console which returned the number 6, but the console on the code challenge returned 0 and I am unable to pass the code challenge. Is there something wrong with the way I interpreted the question? If you passed the code challenge, can you tell me what you did to move on to the next question?

def add_list(list):
    sum = 0
    for items in list:
        sum = sum + items
    print(sum)

add_list([1, 2, 3])

Can anyone please tell me why there must be

       sum = 0

????

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The reason you didn't pass the code challenge is because you used print() instead of return. To get information back from a function, that function has to return it.

Ron Chan
Ron Chan
10,987 Points

Thanks for responding Kenneth. Joshua's comment helped me realize I shouldn't be printing the result from the for loop. Looking forward to your upcoming courses on the Django framework!

greyghost19
greyghost19
471 Points

And "return" was not even mentioned in this video. I dread every challenge because I know it will call up things that we have only gone over once that was 2 or 3 lessons ago. I know this is basic stuff to most people but the coding challenges should cover what's in the video or review in the video things that will be in the challenges.

When asked in Task 1 to "add all the items in the list together," we haven't done that in any video to my knowledge. We certainly haven't practiced it with functions. I get that we're combining 2 different skills but we haven't practiced that combination. To stare at a blank challenge screen and try to recall something that we haven't practiced is intimidating.

Just my thoughts. I recommend building on previous skills in the videos if they are going to be combined for the challenges instead of jumping around. My code is coming out right but I'm just doing what the instructor says. I don't feel like I understand what's going on.

Joshua Johnson
Joshua Johnson
7,552 Points

In Python, sum is a predefined function. I'm not entirely sure if that's the issue you're running into here, but try renaming your 'sum' variable to something like "summation" or "total". 'list' is the same way, so try something like 'a_list' or 'the_list'.

Additionally (and hopefully not to confuse you), you can use += here:

Instead of

sum = sum + items

You can do

sum += items

And achieve the same results!

(Sorry, I'm not too sure how to put my code in a nice easy to read block like yours!)

When in a workspace or challenge though, you should be able to tell if something is already predefined because the color will change; in workspaces, it changes to black, and in the challenges, I believe it changes to pink.

Edit: The problem here is that the challenge is looking for the function to

return

the answer instead of

print

the answer!

The key difference is that if you return the answer, it is actually the output of the function, but if you just print it, it is only printed inside the function. Hopefully this example will make it a bit more clear:

Let's say you have two lists, list_one and list_two. If you have the function return the answer, you can do something like:

total_one = add_list(list_one)
total_two = add_list(list_two)

And then you can utilize those variables elsewhere in your code. But if you only have the function print, then the above code would probably either fail out or each variable would be equal to zero, since the function add_list isn't actually returning anything as output, it's only printing the result within itself. I hope that makes sense!

Ron Chan
Ron Chan
10,987 Points

I appreciate your response Joshua. Renaming the variable "sum" to "total" or renaming the "list" argument didn't make it pass the code challenge. I ran the code on console and it worked fine. You can paste your code here by enclosing them with ``` to mark the beginning and the end of the code snippet. (That is the symbol found beside the number 1 on your keyboard.)

Joshua Johnson
Joshua Johnson
7,552 Points

Ahh, I did that, but it only changed the text style, didn't put it in a nice little block like yours, ah well!

Anyway, I think I see what's actually up, you have it doing print(variable) instead of return(variable). I think the challenge specifically wants the function to return the sum.

Ron Chan
Ron Chan
10,987 Points

I'm not sure what the difference is but you're spot on. Thanks Joshua! I'll flag your reply as best answer if you can help repost your reply as an answer instead of a comment.

Joshua Johnson
Joshua Johnson
7,552 Points

It's alright, I've edited my original answer and hopefully developed a bit on the explanation!