total = 0
for I in range (0,a):
total = total +a[I]+b[I]+c[I]
return total
So, you have a few issues here with your code.
You can't use range(0,a)
. a
is list and not an integer. You would have to use the length of list, or len(a)
.
You also aren't supposed to use capital letters as variables, unless they're constants variables, according to PEP8 standards.
You're trying to return data that isn't inside a function. That won't work.
You could do something like:
def add_vals_from_lists(a,b,c):
total = 0
for i in range(len(a)):
total += sum([a[i], b[i], c[i]])
# The above is a much cleaner way to write total = total +a[I]+b[I]+c[I]
return total
a=[10,20,30,40,50]
b=[60,70,80,90,100]
c=[110,120,130,140,150]
add_vals_from_lists(a,b,c)
Or, you could merely print(total)
instead of using return total
.
But, this will simply add everything to total returning 1200, not what you want.
As Samwise and Pedro noted, you can simply write this in a list-comprehension.
[sum(t) for t in zip(a, b, c)]
zip() - Iterate over several iterables in parallel, producing tuples with an item from each one. For each of these tuples, (10,60,110) for the first one, the list-comprehension will sum() each one. Resulting in a list of [180, 210, 240, 270, 300]
.