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

Alex Wilde
Alex Wilde
2,864 Points

Why is the below giving me a TypeError? It works fine in the interpreter.

The instructions say you may want to use a for loop. Is that necessary?

functions.py
# 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.

add_list = [1, 2, 3]
sum(add_list)

2 Answers

Hi Alex,

That's a great try! You created a list called add_list, but the challenge is asking for a function. You can complete this without using a for loop, but why rob yourself of the practice?

The task will call your function, so no need to define an actual list. Use the argument into your function - call it whatever you want, iterate over each element - adding them up into a total, then return the total value.

def add_list(my_list):
  return #total

Please let me know if this helps or not.

Cheers

sum(add_list)---> return an int str(sum(add_list))--->return an string If you use an function or make print of that to became "The sum of [1, 2, 3] is 6." you should convert int type to an string by ! (because string can't add with an integer) if you want to format it will work fine example: "The sum of [1, 2, 3] is {number}.".format(number=str(sum(add_list)))