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" Functions

Stephan Porsche
Stephan Porsche
2,864 Points

I can't find the right solution of the task "functions.py" but my own code in the editor returns the right output.

My own code written in a editor gives me the right output ("The sum of [1, 2, 3] is 6."), but the check fails.

My code:


list = [1, 2, 3]

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

def summarize(list):
    convert_list = (str(w) for w in list)
    new_sum = add_list(list)
    new_str = str(new_sum)
    print("The sum of ["+", ".join(convert_list)+"] is "+new_str+".")

summarize(list)

Please help!

2 Answers

David Bouchare
David Bouchare
9,224 Points

Hey Stephan,

It does work indeed when I execute the code on my machine, but doesn't in the Treeshouse submission mode.

I'd like to have the opinion of some other folks here, but you really don't need to create those 3 variables convert_list, new_sum and new_str. All of this can be done in one line, replacing the index values with the variables you need:

def summarize(list1):
    return "The sum of {0} is {1}.".format(list1, add_list(list1))

And that should be enough.

Stephan Porsche
Stephan Porsche
2,864 Points

Thx for your hint though! That's a great shortcut :)

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You need to return the string, not print() it.

Stephan Porsche
Stephan Porsche
2,864 Points

Omg, that was obvious my fault. I should better read the task twice next time... Davids line of code helped me alot. I only had to add the "." at the end and now it runs smoothly. Thanks so much guys.

David Bouchare
David Bouchare
9,224 Points

Ah yes my mistake. Cool. And thanks Kenneth!