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

John Coolidge
John Coolidge
12,614 Points

Challenge task 2 of 2

I'm not sure what I'm missing in this challenge task. My code is below:

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

def summarize(list):
    return("The sum of " + list + "is " + add_list(list) + ".")

add_list([1, 2, 3])

What do I need to do in order to get the list from the function summarize to show up as [1, 2, 3] in the string concatenation? Can't I just put the name of the argument there? Or am I thinking of other coding languages? I can never remember what some of these programming languages share, and don't share, with one another.

Thanks!

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You can wrap the list in str():

    return("The sum of " + str(list) + "is " + str(add_list(list)) + ".")

EDIT fixed int to str error

You can also use the format method:

    return("The sum of {} is {}.".format(list, add_list(list)))

EDIT fix syntax error

Note: this code follows the OP code in using list as a variable name. This is not recommended. Use "lst" or "my_list" or other alternative for a variable nam instead of the name of the built-in function list.

John Coolidge
John Coolidge
12,614 Points

I tried both variations, but they tell me that when I try your first option:

TypeError: Can't convert 'int' object to string implicitly

If I try the second, I get:

It looks like task one is no longer passing.

:(

John Coolidge
John Coolidge
12,614 Points

I found a similar answer you wrote on another question like mine. Apparently, the word "list" is a restricted word that can't be used as a variable. I used "lst" instead of "list" as you demonstrated on another question. It worked nicely! Thanks!

This worked really well for me:

def summarize(b_list):
    Y=sum(b_list)
    X=str(b_list)
    return β€˜The sum of {} is {}.’.format(X,Y))
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

That works. However, it bypasses the intent of the challenge by not calling the function add_listto calculate the sum. It is also a bit redundant to format the list as a string in X, then use the format statement which would also format it to a string for you. You could simply replace X in the format statement with b_list.