Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用分形、类和Spumgy的TurtleWorld-Python-调试制作二叉树_Python_Tree_Fractals - Fatal编程技术网

使用分形、类和Spumgy的TurtleWorld-Python-调试制作二叉树

使用分形、类和Spumgy的TurtleWorld-Python-调试制作二叉树,python,tree,fractals,Python,Tree,Fractals,我有一段代码,它读取并解释一个分形描述语言文件,然后使用FractalWorld,这是TurtleWorld的一个特殊版本,它只移除海龟,没有延迟 import math, random, sys from FractalWorld import * world = FractalWorld(width=1000,height=1000,delay=0) Rasmus = Fractal(draw=False) class command(object): def __init__(

我有一段代码,它读取并解释一个分形描述语言文件,然后使用FractalWorld,这是TurtleWorld的一个特殊版本,它只移除海龟,没有延迟

import math, random, sys
from FractalWorld import *

world = FractalWorld(width=1000,height=1000,delay=0)
Rasmus = Fractal(draw=False)

class command(object):
    def __init__(self, cmd, l):
            self.cmd = cmd
            self.l = l

    def execute(self, turtle, length):
            if self.cmd=='lt':
                    lt(turtle, int(self.l[0]))
            if self.cmd=='rt':
                    rt(turtle, int(self.l[0]))
            if self.cmd=='fd':
                    fd(turtle, length)
            if self.cmd=='bk':
                    bk(turtle, length)
            if self.cmd=='scale':
                    lenght = length*float(self.l[0])

class rule(object):
    def __init__(self):
            self.rule={} #we make an empty dictionary

    def newrule(self,leftside,rightside):
            self.rule[leftside]=rightside #we fill the dictionary

    def expand (self,oldlist,depth):
            if depth <= 0:
                    return oldlist 
            newlist=[] # we make an empty new list
            for i in oldlist:
                    if i not in self.rule:
                            newlist.append(i)
                    else:
                            newlist.append(self.expand(self.rule[i],depth-1))
            return newlist

class Fractal(object):
    def __init__(self, start, rules, commands, length, depth):
            self.start = start
            self.rules = rules
            self.commands = commands
            self.length = length
            self.depth = depth

    def draw(self, oldlist):
            for i in oldlist:
                    if type(i) == list:
                            self.draw(i)
                    else:
                            cmd = self.commands[i]
                            cmd.execute(Rasmus, self.length)

def read():
    files = open('tree.fdl')
    commands = {}
    r = rule()
    for line in files:
            line = line.strip()
            oldlist = line.split(' ')
            if oldlist[0] == 'start':
                    start = oldlist[1:]
            elif oldlist[0] == 'rule':
                    r.newrule(oldlist[1], oldlist[3:])
            elif oldlist[0] == 'cmd':
                    cmd = command(oldlist[2], oldlist[3:])
                    commands[oldlist[1]] = cmd
            elif oldlist[0] == 'length':
                    length = int(oldlist[1])
            elif oldlist[0] == 'depth':
                    depth = int(oldlist[1])
    return Fractal(start, r, commands, length, depth)

re = read()

print re.commands.keys()

l = re.rules.expand(re.start , re.depth)
re.draw(l)


wait_for_user()
我的问题是我的解释器有一个bug。似乎解释器没有读取正确的比例,并向下缩放我的线条,以获得下一个深度的树枝。我试过调试它,但没能做到

lenght = length*float(self.l[0])
这条线有两个错误。首先是打字错误。但是,即使名称是正确的,您也会像在函数中做的最后一件事一样为局部变量赋值,这是毫无意义的。您可能应该将其替换为xxx.length,以便调用该函数可以更改其值。我写为xxx的对象可能是调用方可用的分形实例;然后将其作为参数传递,而不是长度。

在哪里可以找到分形世界?
lenght = length*float(self.l[0])