我在IDLE运行时遇到了争论的问题

时间:2013-06-26 02:04:41

标签: python

我的论点如下:

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);

3 个答案:

答案 0 :(得分:1)

这是你的问题:

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

您没有pen参数。试试这个:

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

答案 1 :(得分:0)

它没有将self.pen视为2,而是插入self作为第一个参数。这对所有对象方法函数都是正常的。

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

答案 2 :(得分: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):