0
from graphics import GraphicsWindow

win=GraphicsWindow(400,400)
canvas=win.canvas()
canvas.setFill("black")
#first line 
canvas.drawRect(10,10,10,10)
canvas.drawRect(30,10,10,10)
canvas.drawRect(50,10,10,10)
canvas.drawRect(70,10,10,10)
#second line
canvas.drawRect(20,20,10,10)
canvas.drawRect(40,20,10,10)
canvas.drawRect(60,20,10,10)
canvas.drawRect(80,20,10,10)
#third line 
canvas.drawRect(10,30,10,10)
canvas.drawRect(30,30,10,10)
canvas.drawRect(50,30,10,10)
canvas.drawRect(70,30,10,10)
#fourth line
canvas.drawRect(20,40,10,10)
canvas.drawRect(40,40,10,10)
canvas.drawRect(60,40,10,10)
canvas.drawRect(80,40,10,10)
#fifth line
canvas.drawRect(10,50,10,10)
canvas.drawRect(30,50,10,10)
canvas.drawRect(50,50,10,10)
canvas.drawRect(70,50,10,10)
#sixth line
canvas.drawRect(20,60,10,10)
canvas.drawRect(40,60,10,10)
canvas.drawRect(60,60,10,10)
canvas.drawRect(80,60,10,10)
#seventh line
canvas.drawRect(10,70,10,10)
canvas.drawRect(30,70,10,10)
canvas.drawRect(50,70,10,10)
canvas.drawRect(70,70,10,10)
#eighth line
canvas.drawRect(20,80,10,10)
canvas.drawRect(40,80,10,10)
canvas.drawRect(60,80,10,10)
canvas.drawRect(80,80,10,10)
win.wait()

所以这将创建一个带有 64 个黑色方块的棋盘,但我觉得这似乎是更多的代码而不是必要的。如何更改它,以便仅通过增加 y 坐标(x、y、高度、宽度)而不具有这个疯狂的数量来打印第 1、3、5、7 行和第 2、4、6、8 行代码?

像这样的东西?

canvas.drawRect(10,,10,10).formatrange(y,10,30,50,70)
canvas.drawRect(30,,10,10)
canvas.drawRect(50,,10,10)
canvas.drawRect(70,,10,10)

请记住,我的代码确实有效,我只是想让它看起来更好,更简洁。

4

2 回答 2

0

使用一个简单的循环:

for y in range(1, 9):
    for x in range(4):
        base = 20 if y % 2 == 0 else 10
        canvas.drawRect(base + (x * 20), y * 10, 10, 10)

这里y的范围从 1 到 8(含),乘以 10 得到 10、20、30、40 等。对于每个yx范围从 0 到 3(含),乘以 20 得到 0、20、40 和 60。加 10 或 20 可以得到 10 - 70 或 20 - 80 的范围。

使用itertools.product()可以让你消除一个循环:

from itertools import product

for y, x in product(range(1, 9), range(4)):
    base = 20 if y % 2 == 0 else 10
    canvas.drawRect(base + (x * 20), y * 10, 10, 10)

快速演示只打印使用的坐标:

>>> from itertools import product
>>> for y, x in product(range(1, 9), range(4)):
...     base = 20 if y % 2 == 0 else 10
...     print (base + (x * 20), y * 10)
... 
(10, 10)
(30, 10)
(50, 10)
(70, 10)
(20, 20)
(40, 20)
(60, 20)
(80, 20)
(10, 30)
(30, 30)
(50, 30)
(70, 30)
(20, 40)
(40, 40)
(60, 40)
(80, 40)
(10, 50)
(30, 50)
(50, 50)
(70, 50)
(20, 60)
(40, 60)
(60, 60)
(80, 60)
(10, 70)
(30, 70)
(50, 70)
(70, 70)
(20, 80)
(40, 80)
(60, 80)
(80, 80)
于 2013-09-30T20:35:17.183 回答
0

但我觉得这似乎是更多的代码然后是必要的

你当然有正确的感觉;)

for j in range(10, 90, 10):
    for j in range(10, 90, 20):
    if j % 2 == 1:
        for i in 10, 30, 50, 70:
            canvas.drawRect(i, j, 10, 10)
    else:
        for i in 20, 40, 60, 80:
            canvas.drawRect(i, j, 10, 10)

或者,如果您移动if第二个循环内部:

for j in range(10, 90, 10):
    for i in 10, 30, 50, 70:
        if j % 2 == 1:
            canvas.drawRect(i + 10, j, 10, 10)
        else:
            canvas.drawRect(i, j, 10, 10)

或者有点不同但更短:

for j in range(10, 90, 10):
    start = 10 if j % 2 == 1 else 20
    for i in range(start, 90, 20):
        canvas.drawRect(i, j, 10, 10)
于 2013-09-30T20:39:10.337 回答