gpt4 book ai didi

python - 我在运行类设计时遇到问题,需要帮助找出问题所在

转载 作者:行者123 更新时间:2023-12-01 05:41:57 25 4
gpt4 key购买 nike

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

这是我从空闲状态得到的响应:

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 的地方是问题所在,但我不知道哪里可能有错误。如果有人能给我一个想法或解决方案,那就太好了。谢谢

最佳答案

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

关于python - 我在运行类设计时遇到问题,需要帮助找出问题所在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17310040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com