0

when trying to code something I have an error pop up that says local variable referenced before assignment. I don't understand what it means and can someone please tell me how to fix it?

import random
import turtle
t = turtle.Pen()
colors = ['blue', 'green', 'yellow', 'red', 'cyan', 'black']
turtle.speed(255)
forward_amount = 100
rotate = 25
forward_amount2 = 100
def draw_pattern():        
    for x in range(0, rotate):
        for y in range(0, random_number):
            t.left(degrees)
            TurtleColor = random.choice(colors)
            t.color = TurtleColor
            t.forward(forward_amount)
        t.left(20)

def draw_spiral():        
    for x in range(0, rotate):
        for y in range(0, random_number):
            t.left(degrees)
            forward_amount = forward_amount2
            t.forward(forward_amount)
            forward_amount2 = (forward_amount)+(forward_amount/2)
        



for i in range(1, 5): #how many times you want to loop it
    random_number = random.randint(3, 8)
    degrees = ((random_number - 2)*(180))/(random_number)
    if random_number %2 == 0:
        draw_pattern()
        t.reset()
    else:
        draw_spiral()
        t.reset()
  • tl;dr of the above link: Python thinks your `forward_amount` and `forward_amount2` variables are local variables, when you intend them to be global. You need to tell Python your intentions with the `global` keyword. – Silvio Mayolo Aug 25 '21 at 18:37

0 Answers0