python通过while循环计算1到n的和的代码

1,188 阅读1分钟
把内容过程中比较重要的内容收藏起来,如下内容是关于python通过while循环计算1到n的和的内容。

def sum(n):
    result = 0
    i = 1
    while i <= n:
        result += i
        i += 1
    return result