Python单行for循环

时间:2017-02-01 13:56:29

标签: python for-loop processing nested-loops pyprocessing

我正在使用processing.py

我正在关注这个(Java)

https://www.youtube.com/watch?v=H7frvcAHXps

我想知道我是否可以在python中使用相同类型的for循环

for(int y = 0; y < height; y = y + cellSize):

    for(int x = 0; x < width; x = x + cellSize):

        rect(x, 0, cellSize, cellSize)

我尝试运行代码时收到错误:

processing.app.SketchException: Maybe there's an unclosed paren or quote mark somewhere before this line?

我想在python中使用相同类型的嵌套for循环(在单行上)可能有一种简单但略有不同的方式

1 个答案:

答案 0 :(得分:2)

这在python中是等价的。在range(0, height, cellSize)中,0height是范围的范围,cellSize是计数器增量的数量。

for y in range(0, height, cellSize):
    for x in range(0, width, cellSize):
        rect(x, 0, cellSize, cellSize)