Here’s what I tried… I get a syntax error on the line "context[‘x’] += [ … ]"… basically I want to pass my variables into the function as a dictionary reference… modify a few hash tags and hopefully have a persistent state between function calls.
import numpy as np
def draw_init():
context={'x': [], 'y' : [], 'n' : [], 'count' : 0}
return context
def draw_circle(context, radius):
N0 = 16
for i in range(0, N0-1):
t = float(i) * 2.0 / float(N0) * np.pi;
context['x'] += [float(radius) * np.sin(t)]
context['y'] += [float(radius) * np.cos(t)]
context['n'] += [context['count']]
context['count'] += 1
context = draw_init
draw_circle(context, 500)
draw_circle(context, 200)
draw_circle(context, 100)
>Solution :
import numpy as np
def draw_init():
context={'x': [], 'y' : [], 'n' : [], 'count' : 0}
return context
def draw_circle(context, radius):
N0 = 16
for i in range(0, N0-1):
t = float(i) * 2.0 / float(N0) * np.pi;
context['x'] += [float(radius) * np.sin(t)]
context['y'] += [float(radius) * np.cos(t)]
context['n'] += [context['count']]
context['count'] += 1
context = draw_init()
draw_circle(context, 500)
draw_circle(context, 200)
draw_circle(context, 100)
Note the
context['n'] += [context['count']]
and the
context = draw_init()