2024-04-23 07:19:05 发布
网友
在下面的代码中,total = 0和第5行的目的是什么?这个代码应该打印4和8,但它只返回2!你知道吗
total = 0
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
函数返回小于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)
函数返回小于10的数字的计数。在您的示例中,4和8小于10,因此从算法返回的计数是2,这是正确的。你知道吗
如果您想打印出10以下的数字,请尝试以下操作:
相关问题 更多 >
编程相关推荐