0

我的论点如下:

self.draw(self.pen, side, u0, v0, x, y);

要让它运行,它需要 6 个参数,但它正在将“self.pen”视为 2。它为什么要这样做,我怎样才能让它正确读取它?谢谢

# 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):
                self.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 draw(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);
4

3 回答 3

1

这是你的问题:

def draw(self, side, u0, v0, x, y)

你没有pen参数。尝试这个:

def draw(self, pen, side, u0, v0, x, y)
于 2013-06-26T02:28:40.623 回答
0

它不考虑self.pen为 2,它self作为第一个参数插入。这对于所有对象方法函数都是正常的。

您的定义draw没有pen参数,也不需要一个参数,就像您self.pen在需要时使用的那样。只需将其从函数调用中删除即可。

于 2013-06-26T02:09:52.973 回答
0

如何自我初始化?我假设以下内容:

class Foo(object):
def __init__(self, pen):
    self.pen = pen

def draw(self, pen, side, u0, v0, x, y):
    return pen,side,u0,v0,x,y

如果是这样,以下内容:

foo1 = Foo('ink pen')
bar = foo1.draw(foo1.pen, 'left','0','1','2','3')

应该返回:

('ink pen', 'left', '0', '1', '2', '3')

编辑:在上面添加了附加代码后,很明显问题是:

def draw(self, side, u0, v0, x, y):

它没有“笔”参数。您应该将其更新为:

def draw(self, pen side, u0, v0, x, y):正如我最初在上面列出的那样。

于 2013-06-26T02:15:02.687 回答