如何将海龟设置函数调用到移动海龟的函数中,然后将该函数调用到主函数中?

How can I call a turtles settings function into function for moving the turtle, then call the function to the main?

我知道我的问题很混乱,所以我先把这里的问题弄清楚,然后 post 下面的代码。 如下所示,我正在尝试做的是有一个 tonysetup 函数,它将把一个特殊样式的海龟带到它的指定区域。所以我将 tonysetup 函数调用到 tonyspirograph 函数,并将 torysetup 函数调用到 torystarburst 函数。我会给你另一个相同的代码,主要完成这一切,我的问题是我必须将函数调用到其他函数中。

import turtle
def tonysetup():
    tony = turtle.Turtle()
    tony.shape("turtle")
    tony.pensize(1.05)
    tony.speed(20)
def torysetup():
    tory = turtle.Turtle
    tory.shape("turtle")
    tory.pensize(1.05)
    tory.speed(20)
    tory.penup()
    tory.backward(75)
    tory.left(90)
    tory.forward(25)
    tory.right(90)
    tory.pendown()
def tonyspirograph():
    tonysetup()
    tony.speed(100)
    for i in range(12):
        for color in ("red", "white", "blue"):
            tony.color(color)
            tony.circle(62.5)
            tony.circle(87.5)
            tony.left(10)
    tony.hideturtle()
def torystarburst():
    for i in range(24):
        for color in ("red", "white", "blue"):
            tory.color(color)
            tory.forward(150)
            tory.right(145)
    tory.hideturtle()
def main():
    tonyspirograph()
    torystarburst()
    print("Star Spangled Spirograph: by *** *******")
    print("Thank you veterans!")
main()

现在,当我 运行 这段代码时,它是这样说的 “NameError:名称 'tony' 未在第 26 行定义”

我为退伍军人节制作了下一个代码,这是上一个代码的基础。这一切都是主要的,而不是像我需要的那样调用函数。

import turtle
def main():
    tony = turtle.Turtle()
    tony.shape("turtle")
    tony.pensize(1.05)
    tony.speed(20)
    tory = turtle.Turtle()
    tory.shape("turtle")
    tory.pensize(1.05)
    tory.speed(20)
    for i in range(12):
        for color in ("red", "white", "blue"):
            tony.color(color)
            tony.circle(62.5)
            tony.circle(87.5)
            tony.left(10)
    tony.hideturtle()
    tory.penup()
    tory.backward(75)
    tory.left(90)
    tory.forward(25)
    tory.right(90)
    tory.pendown()
    for i in range(24):
        for color in ("red", "white", "blue"):
            tory.color(color)
            tory.forward(150)
            tory.right(145)
    tory.hideturtle()
    print("Star Spangled Spirograph: by **** *******")
    print("Thank you veterans!")
main()

您在一个函数中定义了 tony,因此目前只能在该函数中识别它。进行设置时,您需要将其传递给 tonyspirograph()。这对我有用:

import turtle
def tonysetup():
    tony = turtle.Turtle() #assign the turtle to the variable tony
    tony.shape("turtle")
    tony.pensize(1.05)
    tony.speed(20)
    return tony #return the turtle
def torysetup():
    tory = turtle.Turtle() #this was missing brackets
    tory.shape("turtle")
    tory.pensize(1.05)
    tory.speed(20)
    tory.penup()
    tory.backward(75)
    tory.left(90)
    tory.forward(25)
    tory.right(90)
    tory.pendown()
    return tory
def tonyspirograph():
    tony = tonysetup() # the setup returns the turtle tony
    tony.speed(100)
    for i in range(12):
        for color in ("red", "white", "blue"):
            tony.color(color)
            tony.circle(62.5)
            tony.circle(87.5)
            tony.left(10)
    tony.hideturtle()
def torystarburst():
    tory = torysetup() # you need to do the same thing
    for i in range(24):
        for color in ("red", "white", "blue"):
            tory.color(color)
            tory.forward(150)
            tory.right(145)
    tory.hideturtle()
def main():
    tonyspirograph()
    torystarburst()
    print("Star Spangled Spirograph: by *** *******")
    print("Thank you veterans!")
main()

至于你的沮丧,我理解,我自己也是新来的。但是当人们不断地询问而不是自己寻找解决方案时,这很烦人。了解如何逐步检查您的代码、阅读错误、参考文档、Google 等。您调试自己的次数越多,您就会学得越快,您就会变得越有能力。然后有一天,当你是一名枪支程序员时,你也会被新手问愚蠢的问题惹恼。

有几个简单的错误使您的代码无法正常工作。首先是 tonysetup()torysetup() 创建海龟,但未能使这些海龟在这些函数之外可用。这可以使用全局变量来完成,或者最好通过将它们创建的海龟的两个函数 return 传递给调用者,调用者将它们存储到一个变量中。

下一个问题是您的 torystarburst() 无法调用您的 torysetup() 函数。你用 tonyspirograph() / tonysetup() 就可以做到这一点,但不能用另一对函数。

最后,您向 turtle speed() 方法传递了错误的值,请重新阅读文档。以下是我为解决上述问题和一些样式问题而对您的代码进行的修改:

from turtle import Screen, Turtle

def tonysetup():
    tony = Turtle()
    tony.shape('turtle')
    tony.pensize(1.05)
    tony.speed('fastest')

    return tony

def torysetup():
    tory = tonysetup()

    tory.penup()
    tory.backward(75)
    tory.left(90)
    tory.forward(25)
    tory.right(90)
    tory.pendown()

    return tory

def tonyspirograph():
    tony = tonysetup()

    for _ in range(12):
        for color in ('red', 'white', 'blue'):
            tony.color(color)
            tony.circle(62.5)
            tony.circle(87.5)
            tony.left(10)

    tony.hideturtle()

def torystarburst():
    tory = torysetup()

    for _ in range(24):
        for color in ('red', 'white', 'blue'):
            tory.color(color)
            tory.forward(150)
            tory.right(145)

    tory.hideturtle()

def main():
    tonyspirograph()
    torystarburst()
    print("Star Spangled Spirograph: by *** *******")
    print("Thank you veterans!")

screen = Screen()

main()

screen.exitonclick()

我将 torysetup() 更改为调用 tonysetup(),因为 tonysetup() 所做的一切都是 torysetup() 最初会做的。