Summing elements in a list

2019-01-30 22:54发布

Here is my code, I need to sum an undefined number of elements in the list. How to do this?

l = raw_input()
l = l.split(' ')
l.pop(0)

My input: 3 5 4 9 After input I delete first element via l.pop(0). After .split(' ') my list is ['5', '4', '9'] and I need to sum all elements in this list.

In this case the sum is 18. Please notice that number of elements is not defined.

标签: python list sum
7条回答
劫难
2楼-- · 2019-01-30 23:50
def sumoflist(l):    
    total = 0    
    for i in l:
        total +=i
    return total
查看更多
登录 后发表回答