这个鳕鱼的目的是什么

2024-04-23 07:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,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

Tags: 代码in目的forreturnifdefcount
1条回答
网友
1楼 · 发布于 2024-04-23 07:19:05

函数返回小于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)

相关问题 更多 >