You should not try to print z or snooze because they have a scope limited by the definition of the function. When you do: sleepy(10,0.001) , then the value 10 is assigned to reps , and the value 0.001 is assigned to snooze .
And then the things you want are done with these variables. In the meantime, a new variable is created with the name z with the scope within the function. And then that value returns. And as soon as the last statement is completed, all variables created inside the definition will be deleted.
So you should do:
a = sleepy(10,0.001) print a
This will print the value of a , which is the value you returned from within the function.
You can also print z if you declare it global, that is:
import time def sleepy(reps, snooze): t = [] for x in range(reps): x = time.time() time.sleep(snooze) y = time.time() global z
Now the value to be returned is in z , and you can print it like this:
sleepy(10,0.001) print z
source share