我有一小段代码来了解如何返回可用于代码其他部分的值。在下面我只想返回变量 z,或者值 snooze。但它不起作用。请有人可以帮我理解为什么这不起作用?
import time
def sleepy(reps, snooze):
t = []
for x in range(reps):
x = time.time()
time.sleep(snooze)
y = time.time()
z = y - x
t.append(z)
print 'difference = ', z*1000
print 'total:', (sum(t)/reps) * 1000
return z
sleepy(10, 0.001)
print z # does not like this.
如果我打印贪睡它也会抱怨。这是为什么?