Question

I created a program to draw a dragon curve using turtle graphics.. but my result doesn't really look like the picture does in the link:

enter image description here

One problem I noticed is that I want to save the produced string into the variable newWord.. but I can't use newWord as a parameter in my function drawit, which actually draws lines based on the string. When I try to do that I get the error "global variable newWord not defined." So in my code I just copied the output of newWord to be drawn, without actually passing the variable that I wanted to pass.

I'm not sure if the problem is with my createWord function or if I'm just not 'drawing enough' in drawit.

import turtle

def createWord(max_it, axiom, proc_rules):

    word = axiom
    t = 1

    while (t < max_it):
        word = rewrite(word, proc_rules)
        t=t+1

    newWord = word

def rewrite(word, proc_rules):

    wordList = list(word)

    for i in range(len(wordList)):
        curChar = wordList[i]
        if curChar in proc_rules:
            wordList[i] = proc_rules[curChar]

    return "".join(wordList)

def drawit(newWord, d, angle):

    newWordLs = list(newWord)
    for i in range(len(newWordLs)):
        cur_Char = newWordLs[i]
        if cur_Char == 'F':
            turtle.forward(d)
        elif cur_Char == '+':
            turtle.right(angle)
        elif cur_Char == '-':
            turtle.left(angle)
        else:
            i = i+1

#sample test of dragon curve

def main():
    createWord(10, 'FX', {'X':'X+YF','Y':'FX-Y'})
    drawit('FX+YF+FX-YF+FX+YF-FX-YF+FX+YF+FX-YF-FX+YF-FX-YF', 20, 90)

if __name__=='__main__': main()
Was it helpful?

Solution

newWord is locally scoped inside of createWord(), so after createWord() is finished, newWord disappears.

Consider creating newWord in the global scope so you can modify it with createWord - or better yet, let createWord() return a value, and set newWord to that value.

I would think that printing "word" and then using it as a parameter in drawit would result in the same thing as using a variable.

It does, but if you want to change the length of your dragon curve, you'll have to copy/paste the string every time instead of simply changing the value of max_it.

Edit: My solution with some sexy recursion (=

import turtle

def dragon_build(turtle_string, n):
    """ Recursively builds a draw string. """
    """ defining f, +, -, as additional rules that don't do anything """
    rules = {'x':'x+yf', 'y':'fx-y','f':'f', '-':'-', '+':'+'}
    turtle_string = ''.join([rules[x] for x in turtle_string])
    if n > 1: return dragon_build(turtle_string, n-1)
    else: return turtle_string

def dragon_draw(size):
    """ Draws a Dragon Curve of length 'size'. """
    turtle_string = dragon_build('fx', size)
    for x in turtle_string:
        if x == 'f': turtle.forward(20)
        elif x == '+': turtle.right(90)
        elif x == '-': turtle.left(90)

def main():
    n = input("Size of Dragon Curve (int): ")
    dragon_draw(n)

if __name__ == '__main__': main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top