Welcome to the Treehouse Community
Looking to learn something new?
Timothy Comeau3,710 Points
Stuck on Python function Challenge Q 1
I'm stuck on what the right approach is for this question. I know this works to get the required answer, but isn't structured according to expectation.
How do I split the list into individual segments and add them together?
example_list=[1,2,3] aw = example_list bw = example_list cw = example_list def add_list(): ans=int(aw+bw+cw) print(ans) add_list()
Inside your function you need a variable that is going to store the sum and then each time through the for loop you're going to add a number from the list to your sum variable. Then you want to return that sum.
def add_list(lst): sum = 0 for num in lst: sum = sum + num return sum
Did you try using a for loop like the challenge recommends?