如何在没有sum()的情况下求和

问题描述 投票:-1回答:1

谁能给我一个关于如何创建自己的求和函数的提示?我不被允许使用sum()......

def add_divisors(start,stop,j):
    xs = (start,stop,j)
    for x in xs:
        #student code
        return sum(range(start,stop,j)) # How to sum without sum()
        #cannot use sum()
    #add_divisors(11, 11, 11) → 11 # 11 is a multiple of 11 
python function built-in
1个回答
0
投票

你唯一能想到的就是评论中提到的sum的功能。

检查一下: -

def add_divisors(start,stop,step):

    ls = list(range(start,stop,step))
    total = 0
    for i in ls:
        total+=i
    return total

print(add_divisors(2,6,1))
© www.soinside.com 2019 - 2024. All rights reserved.