Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 此代码的目的是什么_Python_Python 2.7 - Fatal编程技术网

Python 此代码的目的是什么

Python 此代码的目的是什么,python,python-2.7,Python,Python 2.7,在下面的代码中,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 函数返

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

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

StackOverflow问题需要在问题本身中包含回答问题所需的所有内容,而不是在非现场链接后面,并且作为文本,而不是图像。请解释一下,由于其他原因,这些代码问题通常也是离题的-例如,范围/广度过大-但是,无论怎样,如果没有在问题中包含适当的内容,都会立即被取消资格。我投票将此问题作为离题问题来结束,因为它不包含问题中的代码。另外,一般来说,您应该使用python-2.7或python-3.x标记,但不能同时使用两者。如果问题不是特定于版本的,则仅python标记就足够了。如果您需要一些时间阅读规则并进行适当的编辑,如果您删除了问题,则可以在删除时对其进行编辑,稍后再取消删除。很抱歉,您在这里不受欢迎。查尔斯并不是想让你难受,他是想帮助你把问题做得更好,这样不仅能帮助你,还能帮助未来的读者。这是一个函数。谢谢。语法好吗?非常感谢,我知道了,等我上笔记本电脑的时候我会纠正语法的@Pythonic如果这解决了您的问题,请接受作为答案&快乐编码:我做了,但需要一些时间进行同行评审!