Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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 Turtle_Python_Python 3.x_Turtle Graphics - Fatal编程技术网

在颜色选择中调用用户输入-Python Turtle

在颜色选择中调用用户输入-Python Turtle,python,python-3.x,turtle-graphics,Python,Python 3.x,Turtle Graphics,我不太确定这个问题的措辞是否正确——到目前为止,我掌握的代码如下: import turtle t = turtle.Turtle() colors = {'red', 'blue', 'green', 'black'} def drawSquare(t,x,y,size,fill_color): t.penup() # Pickup turtle t.goto(x,y) # Move Turtle t.pendown() # Drop turtle t.

我不太确定这个问题的措辞是否正确——到目前为止,我掌握的代码如下:

import turtle

t = turtle.Turtle()

colors = {'red', 'blue', 'green', 'black'}

def drawSquare(t,x,y,size,fill_color):

    t.penup() # Pickup turtle
    t.goto(x,y) # Move Turtle
    t.pendown() # Drop turtle
    t.fillcolor(fill_color)
    t.begin_fill()  # Shape Fill begin
    for square in range(0,4):
        t.forward(size) # move forward
        t.right(90) # turn 90 degrees
    t.end_fill() # Shape Fill End

def drawBoard():

    square_color = turtle.textinput("Color Selection", "Enter a color:") # Set color to be user defined
    pos_x = turtle.numinput("Position Selection", "Enter a Position for X") # Set position x to be user defined
    pos_y = turtle.numinput("Position Selection", "Enter a Position for Y") # Set position y to be user defined
    box_size = turtle.numinput("Box Size Selection", "Enter a Box Size (1-50)") # Set box size to be user defined
    for squareOne in range(0,8): # Create a chess board dimension
        for squareTwo in range(0,8): 
            drawSquare(t,pos_x+squareTwo*box_size,pos_y+squareOne*box_size,box_size,square_color)
            square_color = 'Black' if square_color == 'white' else 'white' 
        square_color = 'Black' if square_color == 'white' else 'white' 

drawBoard() # Call drawBoard Function

turtle.done()
在drawBoard函数中,事物围绕着正方形(颜色=‘黑色’线)散开。我的目标是根据分配给变量“square_color”的用户输入来指定正方形的颜色。不幸的是,我真的不太确定如何做到这一点——或者这是否是最有效的方式

如果用户选择了一种颜色,则只有第一个正方形是该颜色,电路板的其余部分后面跟着编码的内容。不知道该如何命名这个问题,否则我很乐意自己去打猎

谢谢大家!

这对我很有用:


t = turtle.Turtle()

colors = {'red', 'blue', 'green', 'black'}

def drawSquare(t,x,y,size,fill_color):

    t.penup() # Pickup turtle
    t.goto(x,y) # Move Turtle
    t.pendown() # Drop turtle
    t.fillcolor(fill_color)
    t.begin_fill()  # Shape Fill begin
    for square in range(0,4):
        t.forward(size) # move forward
        t.right(90) # turn 90 degrees
    t.end_fill() # Shape Fill End

def drawBoard():

    square_color = turtle.textinput("Color Selection", "Enter a color:") # Set color to be user defined
    square_color = 'white' if square_color == 'black' else square_color # Changed here
    pos_x = turtle.numinput("Position Selection", "Enter a Position for X") # Set position x to be user defined
    pos_y = turtle.numinput("Position Selection", "Enter a Position for Y") # Set position y to be user defined
    box_size = turtle.numinput("Box Size Selection", "Enter a Box Size (1-50)") # Set box size to be user defined
    for squareOne in range(0,8): # Create a chess board dimension
        for squareTwo in range(0,8):
            drawSquare(t,pos_x+squareTwo*box_size,pos_y+squareOne*box_size,box_size,square_color if (squareTwo+squareOne)%2 == 0 else 'black') # Changed Here

drawBoard() # Call drawBoard Function

turtle.done()

所以,我只改变了两行: 我已经将
square\u color='white'if square\u color=='black'else square\u color
移动到youn code中的前面,这样,如果用户输入白色,它会将颜色更改为黑色,这样您就可以拥有不同的颜色,而不是一个大的块

我还更改了调用
drawSquare()
函数的位置,以便在调用该函数时,它将决定是添加一个黑色正方形,还是添加一种您选择的颜色。我通过使用模来确定
squareOne
squareTwo
的组合是否为偶数,然后用它来改变颜色。

这对我来说很有效:


t = turtle.Turtle()

colors = {'red', 'blue', 'green', 'black'}

def drawSquare(t,x,y,size,fill_color):

    t.penup() # Pickup turtle
    t.goto(x,y) # Move Turtle
    t.pendown() # Drop turtle
    t.fillcolor(fill_color)
    t.begin_fill()  # Shape Fill begin
    for square in range(0,4):
        t.forward(size) # move forward
        t.right(90) # turn 90 degrees
    t.end_fill() # Shape Fill End

def drawBoard():

    square_color = turtle.textinput("Color Selection", "Enter a color:") # Set color to be user defined
    square_color = 'white' if square_color == 'black' else square_color # Changed here
    pos_x = turtle.numinput("Position Selection", "Enter a Position for X") # Set position x to be user defined
    pos_y = turtle.numinput("Position Selection", "Enter a Position for Y") # Set position y to be user defined
    box_size = turtle.numinput("Box Size Selection", "Enter a Box Size (1-50)") # Set box size to be user defined
    for squareOne in range(0,8): # Create a chess board dimension
        for squareTwo in range(0,8):
            drawSquare(t,pos_x+squareTwo*box_size,pos_y+squareOne*box_size,box_size,square_color if (squareTwo+squareOne)%2 == 0 else 'black') # Changed Here

drawBoard() # Call drawBoard Function

turtle.done()

所以,我只改变了两行: 我已经将
square\u color='white'if square\u color=='black'else square\u color
移动到youn code中的前面,这样,如果用户输入白色,它会将颜色更改为黑色,这样您就可以拥有不同的颜色,而不是一个大的块


我还更改了调用
drawSquare()
函数的位置,以便在调用该函数时,它将决定是添加一个黑色正方形,还是添加一种您选择的颜色。我通过使用模来确定
squareOne
squareTwo
的组合是否为偶数,然后用它来改变颜色。

是否尝试在输入颜色和另一种颜色之间切换?如果我选择红色,它应该在红色和黑色之间交替吗?您是否尝试在输入颜色和其他颜色之间交替?如果我选择红色,它应该在红色和黑色之间交替吗?就是这样!我工作了好几个小时,但我从来没有想到过这一点——这肯定是为了将来的参考。再次感谢你!就是这样!我工作了好几个小时,但我从来没有想到过这一点——这肯定是为了将来的参考。再次感谢你!