-6

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

NAND
  • 645
  • 7
  • 21

1 Answers1

2

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)
nbryans
  • 1,475
  • 16
  • 24
  • 1
    …though you rarely, if ever, use that construction in python. You loop on containers or generators instead. Most probably here you would have a collection of cells and would simply do `for cell in grid:` – spectras Feb 01 '17 at 14:01
  • how do you have a collection of cells? without 2 nested loops? – Kipp Fhtagn Feb 01 '17 at 14:05
  • 1
    @KippFhtagn> either by structuring your objects so you actually have a collection object, or by creating a [generator](http://stackoverflow.com/a/1756156/3212865), separating *how to loop* from *what to do* while looping. – spectras Feb 01 '17 at 14:07
  • @nbryans i got how the code should be equivalent but this doesn't build a table, just a single horizontal line of rectangles – Kipp Fhtagn Feb 01 '17 at 14:13
  • 1
    @KippFhtagn> it should be `rect(x, y, …` he translated your example into proper python but there was a mistake in the loop you put in your question. – spectras Feb 01 '17 at 14:15