错误:返回功能之外。不知道为什么

时间:2019-03-14 01:29:13

标签: python

我不确定我在这里做错了什么,有人可以帮忙吗?我正在尝试编写一个汇总数字列表的函数,并且还有一个用于测试它的测试函数。不幸的是,返回功能无法正常工作,我不确定为什么。

from cLibrary import test

def mysum(xs):
    """ Sum all the numbers in the list xs, and return the total. """
running_total = 0
for x in xs:
    running_total = running_total + x
return running_total

# Add tests like these to your test suite ...
test(mysum([1, 2, 3, 4]) == 10)
test(mysum([1.25, 2.5, 1.75]) == 5.5)
test(mysum([1, -2, 3]) == 2)
test(mysum([ ]) == 0)
test(mysum(range(11)) == 55) # 11 is not included in the list.

1 个答案:

答案 0 :(得分:2)

正如@Barmar所指出的,您需要适当地缩进它:

from cLibrary import test

def mysum(xs):
    """ Sum all the numbers in the list xs, and return the total. """
    running_total = 0
    for x in xs:
        running_total = running_total + x
    return running_total

# Add tests like these to your test suite ...
test(mysum([1, 2, 3, 4]) == 10)
test(mysum([1.25, 2.5, 1.75]) == 5.5)
test(mysum([1, -2, 3]) == 2)
test(mysum([ ]) == 0)
test(mysum(range(11)) == 55) # 11 is not included in the list.