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 trialAndy Bosco
Courses Plus Student 681 PointsI'm stuck.
I can't seem to figure out what I did wrong. See functions.py for my code. I keep getting this message "Bummer! summarize returned the wrong output. Got 'The sum of 6 is 6' instead of 'The sum of [1, 2, 3] is 6.'."
# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
def add_list(num):
total = sum(num)
return total
def summarize(fun):
num = add_list(fun)
total = add_list(fun)
return "The sum of {} is {}".format(num,total)
3 Answers
Mari Johannessen
12,232 PointsHi Andy! In the last line in your last function you're formatting 'num' and 'total' and they are both calling the same function in the last function, so they will give the same output. If you change that to the value that's given to the summarize function, you will see that the code now works! Hopefully I explained it good enough for you to understand :)
def add_list(num):
total = sum(num)
return total
def summarize(fun):
num = add_list(fun)
total = add_list(fun)
return "The sum of {} is {}".format(fun,total)
Gene Osborne
8,630 PointsTake notice that the variable that has the wrong value ('num') is being assigned the value of add_list(fun), which returns the sum of the list. The error you are receiving is asking for the list itself. So instead of passing the 'num' into the 'format' method, try passing the name of the list ('fun').
Andy Bosco
Courses Plus Student 681 PointsHi Gene, I'm new to Python and don't understand what do you mean by "So instead of passing the 'num' into the 'format' method, try passing the name of the list ('fun')."
Gene Osborne
8,630 PointsString are objects. Objects are entities that have methods that access or change their values. A method is simply a function that is attached to the object. When you type '.format(var) ' onto the end of a String, you are calling the String's 'format' method.
if you change your statement to ' return "The sum of {} of {}".format(fun,total) ' The method is simply changing the value of the String by replacing the {}'s with the values you choose. In this case, you use the variable named 'fun' because it is wanting you to display the whole list in place of the first {}.
Andy Bosco
Courses Plus Student 681 PointsThanks Mari. It works now.
Andy Bosco
Courses Plus Student 681 PointsAndy Bosco
Courses Plus Student 681 PointsHi Mari, thanks for the quick respond. I changed my last line to "return "The sum of {} is {}".format(add_list(num),summarize(fun))" and I'm getting this error now "Bummer! TypeError: 'int' object is not iterable"
Mari Johannessen
12,232 PointsMari Johannessen
12,232 PointsHi again,
Try changing it to 'return "The sum of {} of {}".format(fun,total)'. You don't need to call the function again inside the function. You're already calling add_list(fun) inside the function which will be assigned to the variable 'total', so this variable is ready to be used in the return statement.