用python循环绘制棋盘

用python循环绘制棋盘,python,turtle-graphics,Python,Turtle Graphics,我试图用乌龟图形画一个棋盘,但我不知道如何循环穿过每一个方块把它画成黑色 for each_row in range(number_of_rows): for each_column in range(number_of_columns): if (each_column % 2) ==0: #this is the line that's doubtful draw_black_box() else:

我试图用乌龟图形画一个棋盘,但我不知道如何循环穿过每一个方块把它画成黑色

for each_row in range(number_of_rows):
    for each_column in range(number_of_columns):
        if (each_column % 2) ==0:                #this is the line that's doubtful
            draw_black_box()
        else:
            draw_white_box()
        goto_next_box_pos()
    goto_next_row_pos()
画盒子很好,但我觉得我画错了


现有函数goto_next_box_pos()负责将海龟重新定位到要绘制行中下一个方框的位置。现有函数goto_next_row_pos()负责将海龟重新定位到要绘制下一行的位置

这样做的问题是,它只会在更改列时交替颜色,而不会更改行-因此,最终将使用条纹板,而不是网纹板。您需要更改条件以包括行号,这样行和列都可以交替使用颜色。碰巧的是,简单地将这些数字相加就可以得到您想要的:

if ((each_row + each_column) % 2) == 0:

这是构造代码的一种非常合理的方法。好的,但是在第3行中,我应该输入>>if(每行%2)==0:吗?还是这样可以