1
    # Classy Mandelbrot Patterns.

import turtle;
import colorsys;

# A Mandelbrot patterns class:
class MandelbrotPatterns:
    # Initialize the pen, and determine the window width and height:
    def __init__(self):
        self.pen = turtle.Pen(); 
        self.width = self.pen.window_width();
        self.height = self.pen.window_height();

# Given numbers self, u0, v0, and side, design a pattern:
def mandelbrot(self, u0, v0, side):
    self.pen.tracer(False);   
    for x in range(-self.width/2, +self.width/2):
        for y in range (-self.height/2, +self.height/2):
            draw(self.pen, side, u0, v0, x, y);
        if (x % 20 == 0):
            self.pen.tracer(True); self.pen.tracer(False);
    self.pen.tracer(True);

# Draw in color a pattern element at point (x, y):
def selfdraw(self, side, u0, v0, x, y):
    maxCount = 25;
    u = u0 + x * (side / 100.0);
    v = v0 - y * (side / 100.0);
    a = 0 ; b = 0; count = 0; 
    while (count < maxCount and a * a + b * b <= 4):
        count = count + 1;
        a1 = a * a - b * b + u;
        b1 = 2 * a * b + v;
        a = a1; b = b1;
    ez = float(count) / maxCount;
    color = colorsys.hsv_to_rgb(ez, ez, ez);
    self.pen.color(color);
    self.pen.up(); self.pen.goto(x, y); self.pen.down();
    self.pen.forward(1);

当我运行模块时,我会测试这些信息以确保它有效:

>>> mb = MandelbrotPatterns();
>>> mb.mandelbrot(u0= -0.5, v0 = 0, side = 1);

这是我从 Idle 得到的回复:

Traceback (most recent call last):
      File "<pyshell#10>", line 1, in <module>
        mb.mandelbrot(-0.5, 0, 1)
    AttributeError: MandelbrotPatterns instance has no attribute 'mandelbrot'

我在想我在哪里定义 mandelbrot 是问题,但我看不出哪里可能有错误。如果有人能给我一个很棒的想法或解决方案。谢谢

4

1 回答 1

2

您的方法需要进一步缩进一级。实际上,它们是与类定义在同一级别的函数。

于 2013-06-26T01:14:51.607 回答