Python 花与龟图形

Python Flowers with Turtle(s) Graphics

我在高中的编程中使用海龟图形 class,该项目是按照老师演示的一些指导方针和功能制作一朵花。我在一个小时内完成了,现在我想用更多的海龟一次画多朵花,但是我不能让海龟使用新定义的函数,老师也没有时间和我见面在一个上讨论我该怎么做

因此,经过大约一周的搜索(对于一些我不知道如何正确提问的问题,更不用说寻找了),我将前往我最喜欢的问题网站。因此,如果有人可以帮助我或至少为我指明正确的方向,我将不胜感激。

   import turtle

tod = turtle.Turtle
tina = turtle.Turtle
tony = turtle.Turtle
trixie = turtle.Turtle
tron = turtle.Turtle


def petal():
    for i in range(90):
        self.start = self.pos()
        self.fd(1)
        self.rt(1)
    self.rt(90)
    for i in range(90):
        self.fd(1)
        self.rt(1)

def stem(self):
    self.pencolor('green')
    self.fd(220)

def flowerhead(self):
    for i in range(9):
      begin_fill()
      petal()
      self.lt(230)
      end_fill()

def stempetal(self):
    self.seth(90)
    self.rt(15)
    fillcolor('green')
    begin_fill()
    petal()
    end_fill()

def flower1(self):
    flowerhead()
    stem()
    stempetal()

def flower2(self):
    flowerhead()
    self.stem()


tod.flower()

运行 给出错误

Traceback (most recent call last):
  File "C:\Users\first.last\Desktop\programming\trig class\testflowerclass.py", line 49, in <module>
    tod.flower()
AttributeError: type object 'Turtle' has no attribute 'flower'

您正在使用但不熟悉的 (python) 编程领域似乎是 类、实例和继承。这里似乎是 类 的入门级教程:https://en.wikibooks.org/wiki/A_Beginner%27s_Python_Tutorial/Classes

在没有先介绍这个主题的情况下就分配这个主题似乎很刻薄。

您可能需要按照以下方式构建代码:

from turtle import Turtle

class MyTurtle(Turtle):
    def my_method(self):
        self.method_defined_in_turtle()
        self.other_method_defined_in_turtle()

todd = MyTurtle()
todd.my_method()