Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Trouble with Dictionaries, cTurtle, and a little Recursion
    primarykey
    data
    text
    <p>I'm supposed to do the following</p> <blockquote> <p>Change the applyProduction function so that the dictionary of rules maps each symbol not to a single replacement string, but to a list of alternative strings, one of which is randomly chosen each time a replacement needs to be performed. (You can randomly choose an element of a list using the choice function within the random module.) As in the original version of applyProduction, some symbols may not be listed in the rules at all, in which case they should be left unchanged.</p> </blockquote> <p>The applyProduction function looks like</p> <pre><code>def applyProduction(axiom,rules,n): for i in range(n): newString = "" for ch in axiom: newString = newString + rules.get(ch,ch) axiom = newString return axiom </code></pre> <p>And the one that I've done is</p> <pre><code>def applyProduction(axiom,rules,n): for i in range(n): newString = "" for ch in axiom: listOfRules = rules.get(ch) doIt = random.choice(listOfRules) newString = newString + doIt axiom = newString return axiom </code></pre> <p>So I'm trying to get all of the values of the dictionary associated with the letter ch. And then choose a random on of them. I think what I'm trying to do is the way to do it and that it should work, but when I do it I get the error</p> <pre><code>Traceback (most recent call last): File "&lt;pyshell#18&gt;", line 1, in &lt;module&gt; lsystem('F-F-F',{'F':'F-F-F-GG', 'F':'F+G++F', 'G':'GG'}, 3,(-100,-100),0,120,25) File "C:\Python33\lsystems.py", line 61, in lsystem instructions = applyProduction(axiom,rules,depth) File "C:\Python33\lsystems.py", line 16, in applyProduction doIt = random.choice(listOfRules) File "C:\Python33\lib\random.py", line 249, in choice i = self._randbelow(len(seq)) TypeError: object of type 'NoneType' has no len() </code></pre> <p>And i don't really know what that means. Any help is appreciated. </p> <p>Other functions involved.</p> <pre><code>def drawLS(aTurtle,instructions,angle,distance): stateSaver = [] for cmd in instructions: if cmd == 'F': aTurtle.forward(distance) elif cmd == 'G': aTurtle.up() aTurtle.forward(distance) aTurtle.down() elif cmd == 'B': aTurtle.backward(distance) elif cmd == '+': aTurtle.right(angle) elif cmd == '-': aTurtle.left(angle) elif cmd == '[': pos = aTurtle.position() head = aTurtle.heading() stateSaver.append((pos,head)) elif cmd == ']': pos,head = stateSaver.pop() aTurtle.up() aTurtle.setposition(pos) aTurtle.setheading(head) aTurtle.down() </code></pre> <p>.</p> <pre><code>def lsystem(axiom,rules,depth,initialPosition,heading,angle,length): aTurtle = Turtle() aTurtle.speed(0) # this line improves on Listing 9.8 aTurtle.shape('blank') # this line improves on Listing 9.8 aTurtle.up() aTurtle.setposition(initialPosition) aTurtle.down() aTurtle.setheading(heading) ## The variable "instructions" was called "newRules" in Listing 9.8, ## which was misleading regarding what kind of thing it names. instructions = applyProduction(axiom,rules,depth) drawLS(aTurtle,instructions,angle,length) aTurtle.exitOnClick() </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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