summing only the numbers contained in a list

前端 未结 6 1776
猫巷女王i
猫巷女王i 2020-12-17 20:27

Give a method that sums all the numbers in a list. The method should be able to skip elements that are not numbers. So, sum([1, 2, 3]) should be

6条回答
  •  情歌与酒
    2020-12-17 21:06

    def foo(list):
    dict= "ABCDEFGHIJKLMN"
    n=0
    for i in range(0,len(list)-1):
        if str(list[i]) in dict:
            ""
        else:    
            n= n+list[i]
    return n
    print foo([1,2,3,4,5,6,"A","B"])
    

提交回复
热议问题