在Python中嵌入函数

在Python中嵌入函数,python,function,turtle-graphics,Python,Function,Turtle Graphics,我使用Python 3中的海龟图形创建了一个程序,该程序绘制美国国旗: import turtle import time import random def draw_rectangle(length, height): turtle.up() x = -150 y = 150 C = height*(7/13) D = length*(2/5) L = stripe_width = float(round(height/13,1))

我使用Python 3中的海龟图形创建了一个程序,该程序绘制美国国旗:

import turtle
import time
import random

def draw_rectangle(length, height):
    turtle.up()
    x = -150
    y = 150
    C = height*(7/13)
    D = length*(2/5)
    L = stripe_width = float(round(height/13,1))

    ## Draw rectangle first.
    turtle.color(0,0,0)
    turtle.begin_fill()
    turtle.setpos(x,y)
    turtle.down()
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.end_fill()

    ## Then draw the stripes.
    x1 = -150
    y1 = 150-L
    for z in range(13):
        if z%2 == 0:
            r = s = t = 0
        else:
            r = s = t = 1
        turtle.up()
        turtle.speed(100)
        turtle.setpos(x1,y1)
        turtle.setheading(90)
        turtle.down()
        turtle.color(r,s,t)
        turtle.begin_fill()
        turtle.forward(L)
        turtle.right(90)
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(L)
        turtle.right(90)
        turtle.forward(length)
        turtle.end_fill()
        y1 -= L

    ## Finally draw the stars rectangle overlapping the stripes, next is stars.
    x2 = -150+D
    y2 = 150.5-C
    turtle.up()
    turtle.setpos(x2,y2)
    turtle.down()
    turtle.color(0,0,0)
    turtle.begin_fill()
    turtle.forward(D)
    turtle.right(90)
    turtle.forward(C)
    turtle.right(90)
    turtle.forward(D)
    turtle.right(90)
    turtle.forward(C)
    turtle.end_fill()
    turtle.up()

    turtle.bye
    draw_star(-length, height)

def draw_star(l, h):
    for z in range(50):
        if z < 7:
            row = 140
            draw_starrows(row)
        if z < 14:
            row = row - 20
            draw_starrows(row)
        if z < 21:
            row = row - 20
            draw_starrows(row)
        if z < 28:
            row = row - 20
            draw_starrows(row)
        if z < 35:
            row = row - 20
            draw_starrows(row)
            ## This gets the turtle pen out of the way at the very end.
            turtle.up()
            turtle.setpos(-180,100)
        break

def draw_starrows(row):
    x = -160
    y = 150
    for z in range(10):
        x += 15
        turtle.up()
        turtle.color(1,1,1)
        turtle.speed(100)
        turtle.setpos(x,row)
        turtle.begin_fill()
        turtle.down()
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.end_fill()
    turtle.bye

##def get_color():
##    r = g = b = 0
##    color = r = g = b
##    return color

def draw_flag():
    A = 200
    height = int(A)
##    length = height*1.9
##    C = height*(7/13)
##    D = length*(2/5)
##    E = F = union_height/10
##    G = H = union_length/12
##    stripe_width = height/13
##    diameter_star = stripe_width*(4/5)
    draw_rectangle(height*1.9, height)

draw_flag()

可以从函数返回多个值

def get_color():
    r = g = b = 0
    return r, g, b

red, green, blue = get_color()
您可以将多个返回值传递给如下函数:

turtle.color(*get_color())

get\u color()
返回三个值,作为
turtle.color

1的3个参数。我认为您不需要这样的函数,您可以非常简单地设置颜色(对于单个标志):

或者任何你想要的颜色

编辑:那么我认为应该是这样:

def get_color(color):
    c = color
    return c

a = get_color("blue")
您可以这样使用它:

turtle.color(a)
2.您的窗口未关闭,因为它应该是:

turtle.bye()
或者在您的代码中只有

turtle.bye
此外,在您的代码中,turtle.bye出现两次并且出现在错误的位置-您应该将其放在代码的末尾(至少对我来说,它是这样工作的):


我希望它能有所帮助。

您希望代码看起来像什么?我不知道你所说的用
get\u color(color)
替换所有
turtle.color(0,0,0)
是什么意思。哦,对不起,我的意思是不用手动输入颜色(0,0,0)和(1,1,1),我想要一个函数将它们预定义到一个变量中,然后将该变量分配给turtle.color(),我已经编辑了答案。这有帮助吗?谢谢你抓住了海龟。再见()问题,但是,这是一个项目,其中一个要求是使用函数定义颜色,否则我已经手动添加了颜色。啊,好吧,那么如果我想使用两种颜色,r,g,b=0和r,g,b=1,我必须创建两个单独的函数吗,或者我可以用不同的变量返回它两次(r,g,b=0表示一次,然后对于ex.d,e,f=1表示下一次返回)这样行吗?可能类似于
get_color(stripe)
?一个函数可以有多个
return
语句。使用
if
语句或其他一些控制流来决定使用哪个
return
。我现在已经创建了必要的循环:。唯一的问题是我得到了一个错误,说draw_rectangle(长度、高度、颜色):接受3个参数,但只给出1个参数,我不知道如何将“color”与描述中的其他函数draw_rectangle和draw_star合并。
turtle.color(a)
turtle.bye()
turtle.bye
draw_flag()
turtle.bye()