在下面的代码中,total = 0
和第5行的目的是什么?此代码应打印4和8,但它只返回2!
def count_small(numbers):
total = 0
for n in numbers:
if n < 10:
total = total + 1
return total
lost = [4, 8, 15, 16, 23, 42]
small = count_small(lost)
print small
答案 0 :(得分:1)
该函数返回小于10的数字的计数。在您的示例中,4和8低于10,因此从算法返回的计数为2,这是正确的。
如果您想打印出10以下的数字,请尝试:
def print_small(numbers):
for n in numbers:
if n < 10:
print n
lost = [4, 8, 15, 16, 23, 42]
print_small(lost)