Imagine:
str = input( Muchos numeros separados por ',' : )
list = str.split(', ')
total = list(0) + list(1) + list(2) ... (Number of indexes in the anwer)
How can I make it sum all of the indexes of a list?
Join Stack Overflow to learn, share knowledge, and build your career.
First map to int
then sum
:
print(sum(map(int,input("Muchos numeros separados por ',' :").split(', '))))
Just iterating over list you can find sum of all elements in list:
mylist = [1,2,3,4,5];
sum = 0;
for i in mylist: # i will be 1,2,3,4,5 each loop
sum+=i;
print(sum); # sum = 15
If its not your answer looking for, can you describe your problem better?
a = list(map(int, input("Enter list values:").split()))
sum1 = 0
for x in a: # iterating over the list
sum1 += x
print(sum1)
str
andlist
because those builtins will we overwritten. ("Never" means: if you can't present a very, very, very good reason to do that) – Matthias Nov 25 '20 at 16:12