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 trialAtidaishe Chinodakufa
2,323 Pointsfunctions
whats wrong in my code. preview is just but a black screen.
num_list[]
def add_list(num_list)
for num in num_list:
return num+num
add_list()
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
2 Answers
Dan Johnson
40,533 PointsFunctions can only return once. When you use return you exit out of the function right at that point. In order to sum up the list of numbers using a for loop you'll need to use a local variable:
def add_list(number_list): #Function signatures need to end with a colon to start the block.
total = 0
for number in number_list:
total += number
return total #Everything in the same block should have consistent indentation.
You don't need to call the add_list function yourself for this challenge. But if you did, you'd need to supply it with a list of numbers and also remove the indentation as it currently resides in the function definition.
Atidaishe Chinodakufa
2,323 PointsThanks for the help! It really worked.