Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking a Sierpinski triangle using fractals, classes and swampy's TurtleWorld - Python
    primarykey
    data
    text
    <p>I have this Python code: </p> <pre><code>import math, random, sys from swampy.TurtleWorld import * world = TurtleWorld() Rasmus = Turtle() Rasmus.delay = 0.000002 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 &lt;= 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 command(object): def __init__(self, cmd, length): self.cmd = cmd self.length = length def execute(self, turtle, length): if self.cmd=='lt': lt(turtle, int(self.length[0])) if self.cmd=='rt': rt(turtle, int(self.length[0])) if self.cmd=='fd': fd(turtle, length) if self.cmd=='bk': bk(turtle, length) if self.cmd=='scale': length = length*float(self.length[0]) class Fractal(object): def __init__(self, rules, commands, start, length, depth): self.rules = rules self.commands = commands self.start = start 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('sierpinski2.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() length = re.rules.expand(re.start , re.depth) re.draw(l) wait_for_user() </code></pre> <p>which should extract information from a sierpinski.fdl file, which looks like this: </p> <pre><code>start F L F L F L rule F -&gt; F L F L F L F F length 8 depth 5 cmd F fd cmd L lt 120 </code></pre> <p>My problem is that when I run this, it gives me this error:</p> <pre><code>Traceback (most recent call last): File "C:\Python27\PythonScripts\swampy-2.1.1\swampy\Projekt del 2.py", line 81, in &lt;module&gt; print re.commands.keys() AttributeError: 'rule' object has no attribute 'keys' </code></pre> <p>I know that the problem lie in the:</p> <pre><code>re = read() print re.commands.keys() length = re.rules.expand(re.start , re.depth) re.draw </code></pre> <p>Part of the code, where I enable the execution of the program, but I can't seem to fix it.</p> <p>I am using python 2.7.5 </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload