Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我想做一个棋盘_Python - Fatal编程技术网

Python 我想做一个棋盘

Python 我想做一个棋盘,python,Python,我正在尝试制作一个棋盘,使用可变的color_值,并使用模数操作符来确定正方形是红色还是黑色。我不确定如何使用模运算符 speed(0) penup() setposition(-200,-200) pendown() color_value = 0 def red_square(): color("red") for i in range(4): forward(40) left(90) def black_square()

我正在尝试制作一个棋盘,使用可变的color_值,并使用模数操作符来确定正方形是红色还是黑色。我不确定如何使用模运算符

speed(0)
penup()
setposition(-200,-200)
pendown()
color_value = 0

def red_square():
    color("red")
    for i in range(4):
        forward(40)
        left(90)
def black_square():
    begin_fill()
    for i in range(4):
        forward(40)
        left(90)

def make_squares():
    for i in range(2):
        if color_value / 2 == 0:
            begin_fill()
            red_square()
            end_fill()
        elif color_value / 2 != 0:
            black_square()
        color_value+1
        penup()
        forward(40)
        pendown()
            

make_squares()

我不知道您在哪里或为什么尝试使用模运算符,但为了更广泛地回答您关于如何使用模运算符的问题,模运算符
%
放在两个数字之间,当第一个数字除以第二个数字时,表达式返回余数。例如:

3 % 2   # Returns 1 because 1 is the remainder when 3 is divided by 2
使用:


当且仅当
n
k
的精确倍数时,计算true。在初等数学中,这称为除法的余数。

只要将
如果颜色值/2==0
更改为
如果颜色值%2==0

并将
color\u值+1
更改为
color\u值+=1

确保你的方块不重叠

这是你的完整代码
我试图在if语句中使用模运算符,例如,如果color_值%2==0,则会根据颜色的剩余部分使正方形变为红色或黑色_value@IcoSli只要将
/
更改为
中的
%
,如果color\u value/2==0,我就更改了,但是当我运行代码时,它只生成红色方块。@IcoSli好的,我已经更新了我的答案,有完整的代码。
n % k == 0
    speed(0)
    penup()
    setposition(-200,-200)
    pendown()
    color_value = 0
    
    def red_square():
        color("red")
        for i in range(4):
            forward(40)
            left(90)
    def black_square():
        begin_fill()
        for i in range(4):
            forward(40)
            right(90)
    
    def make_squares():
        for i in range(2):
            if color_value % 2 == 0:
                begin_fill()
                red_square()
                end_fill()
            elif color_value % 2 != 0:
                black_square()
            color_value+=1
            penup()
            forward(40)
            pendown()
                
    
    make_squares()