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 trialDaniel Larouche
7,675 Pointshow to put a list in a function
I don't understand how to put a list as an argument in a function.
# 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.
1 Answer
LaVaughn Haynes
12,397 PointsI've only had VERY BASIC exposure to Python so I don't know if this is the BEST way to send a list to a function but it works:
# add numbers
numberList = [1, 2, 3]
def add_numbers(numbers):
return sum(numbers)
print add_numbers(numberList)
You can paste the code here, click the save icon, then click "execute" to verify
good luck!
LaVaughn Haynes
12,397 PointsLaVaughn Haynes
12,397 PointsAlso, FYI, if you use the Python v3.3 compiler on that site you have to use parenthesis with print. The version above uses v2.7
print( add_numbers(numberList) )