2017-02-01 210 views
-6

我使用processing.py的Python for循環

我在下面這個嘖嘖(JAVA)

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

,我想知道如果我可以使用相同類型的單行環在python

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

+0

嗨,歡迎來到SO,'for'循環在Python中不起作用。請檢查基本教程,如https://wiki.python.org/moin/ForLoop –

+3

您也遺漏了'public static void main'位。 –

回答

2

可能是一個簡單的,但略有不同的方式做使用同一種嵌套的for循環(在同一行)這將是蟒蛇的等價物。在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) 
+1

...雖然你很少,如果有的話,在python中使用該構造。您可以循環使用容器或發電機。最有可能的是,在這裏你將擁有一組單元格,並且可以簡單地在網格中執行單元格操作:' – spectras

+0

你如何擁有一組單元格?沒有2個嵌套循環? –

+1

@KippFhtagn>或者通過構建對象,讓你實際上擁有一個集合對象,或者通過創建一個[generator](http://stackoverflow.com/a/1756156/3212865),分離*如何循環*從*什麼做*循環時。 – spectras