【问题标题】:How can I call a turtles settings function into function for moving the turtle, then call the function to the main?如何将海龟设置函数调用为移动海龟的函数,然后将该函数调用到主函数?
【发布时间】:2021-12-05 20:51:59
【问题描述】:

我知道我的问题令人困惑,所以我会在这里清理一下,然后发布下面的代码。 如下所示,我想要做的是有一个 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: name '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()

【问题讨论】:

    标签: python function python-turtle


    【解决方案1】:

    您已经在函数中定义了 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()
    

    至于你的不满,我明白,我自己也是新来的。但是当人们不断地询问而不是为自己寻找解决方案时,这很烦人。学习如何一步一步地检查你的代码,阅读错误,参考文档,谷歌等。你自己调试的越多,你学得越快,你就会变得越有能力。那么有一天,当你是一名枪支程序员时,你也会被新手提出愚蠢的问题所惹恼。

    【讨论】:

      【解决方案2】:

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

      下一个问题是您的torystarburst() 未能调用您的torysetup() 函数。使用 tonyspirograph() / tonysetup() 可以做到这一点,但使用另一对功能则不然。

      最后,您将错误的值传递给海龟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() 最初所做的。

      【讨论】:

        猜你喜欢
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多