Search code examples
pythonfor-loopprocessingnested-loopspyprocessing

Python single line for loops


I'm using processing.py

I was following this tut (Java)

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

and I'm wondering if I can use the same kind of for loop in python

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

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

        rect(x, 0, cellSize, cellSize)

I receive an error when I try to run the code:

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

I guess there's probably an easy but slightly different way to do the use the same kind of nested for loops (on a single line) in python


Solution

  • This would be the equivalent in python. In range(0, height, cellSize), 0 and height are the bounds of the range, and cellSize is how many out counter increments.

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