I want to be able to return the value assigned to a variable assigned to an active function, but if that function is not active, I want to be able to return 0
for the same variable, but assigned to a different function. The code block below is what I want to do this with:
def draw(x):
if not hasattr(draw, 'counter'):
draw.counter = 0
draw.counter += 1
global drawing
q.put(x)
process = False
drawingLock.acquire()
if not drawing:
process = True
drawing = True
drawingLock.release()
if process:
if not q.empty():
x()
drawingLock.acquire()
drawing = False
drawingLock.release()
print('`draw` has been called {} times'.format(draw.counter))
h = draw.counter
def walk():
if not hasattr(walk, "counter"):
walk.counter = 0
walk.counter += 1
penup()
forward(letter_width + space_width)
pendown()
print('`walk` has been called {} times'.format(walk.counter))
global w
w = 0 if walk is False else walk.counter
onkey(walk, "space")
def draw_newline():
# This funtion will pick up the turtle and move it to a second line below HELLO
penup()
goto(xcor() -(((space_width + letter_width)*(draw.counter)) + (space_width * w)) , ycor() -(letter_height + letter_height/2))
pendown()
def handle_enter():
draw.counter = 0
walk.counter = 0
def Carriage_functions():
draw_newline()
handle_enter()
onkey(Carriage_functions, "Return")
As you can see, this walk()
function:
def walk():
if not hasattr(walk, "counter"):
walk.counter = 0
walk.counter += 1
penup()
forward(letter_width + space_width)
pendown()
print('`walk` has been called {} times'.format(walk.counter))
global w
w = 0 if walk is False else walk.counter
onkey(walk, "space")
draws a space in the turtle graphics window every time SPACE
is pressed on the keyboard, and it also counts how many times it has been executed through:
if not hasattr(walk, "counter"):
walk.counter = 0
walk.counter += 1
Then, I assign this value to the globally declared variable 'w' to be used in the next function (draw_newline()
) like this:
global w
w = 0 if walk is False else walk.counter
The issue is that whenever I assign the w
to the draw newline()
's goto()
function, like so:
goto(xcor() -(((space_width + letter_width)*(draw.counter)) + (space_width * w)) , ycor() -(letter_height + letter_height/2))
I get this error:
in draw_newline
goto(xcor() -(((space_width + letter_width)*(draw.counter)) + (space_width * w)) , ycor() -(letter_height + letter_height/2))
NameError: name 'w' is not defined
even though w
is globally defined! What am I doing wrong here?
EDIT: I have even tried this:
def walk():
if not hasattr(walk, "counter"):
walk.counter = 0
global counter
walk.counter += 1
penup()
forward(letter_width + space_width)
pendown()
print('`walk` has been called {} times'.format(walk.counter))
global h
h = walk.counter
if walk == False:
w = 0
elif walk == True:
w = h
and still no luck. I am just getting this error:
NameError: free variable 'w' referenced before assignment in enclosing scope
Even when I put the same code block after the draw_newline()
function, I still get the same error.