如果函数处于活动状态,则返回函数变量的值;如果未调用函数,则返回0作为变量,但是调用相同变量的其他函数

时间:2015-12-17 22:17:57

标签: python function variables python-3.x

我希望能够返回分配给分配给活动函数的变量的值,但是如果该函数未激活,我希望能够返回{strong>相同的<{1}} 变量,但分配给不同的功能。下面的代码块是我想要的:

0

如您所见,此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") 函数:

walk()

每次在键盘上按下 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") 时在乌龟图形窗口中绘制一个空格,它还会计算它执行的次数:

SPACE

然后,我将此值赋给全局声明的变量'w',以便在下一个函数(if not hasattr(walk, "counter"): walk.counter = 0 walk.counter += 1 )中使用,如下所示:

draw_newline()

问题是每当我将global w w = 0 if walk is False else walk.counter 分配给w的{​​{1}}函数时,就像这样:

draw newline()

我收到此错误:

goto()

即使goto(xcor() -(((space_width + letter_width)*(draw.counter)) + (space_width * w)) , ycor() -(letter_height + letter_height/2)) 是全局定义的!我在这里做错了什么?

编辑:我甚至试过这个:

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

仍然没有运气。我刚收到这个错误:

w

即使我在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 函数后面放了相同的代码块,我仍然会遇到同样的错误。

0 个答案:

没有答案