rich
rich

Reputation: 61

Unable to return a value from a function

I have a small piece of code to understand how to return values that can be used in other sections of the code. In the following i only want to return the variable z, or the value snooze. But it does not work. Please can someone help me to understand why this will not work?

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.

If I print snooze it also grumbles. Why is that?

Upvotes: 6

Views: 227

Answers (5)

Mahesh Nadar
Mahesh Nadar

Reputation: 487

z is a local variable.when you return z it not actually returns variable z instead its returns the value which is present in z so u need to store it in another variable and print that variable
or you can just use

print sleepy(10, 0.001)

Upvotes: 1

IcyFlame
IcyFlame

Reputation: 5199

You should not try to print z or snooze because they have a scope that is limited to 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 that you want are done with these variables. In the meantime a new variable called z is created with the scope inside the definition of the function. And then this value is returned. And as soon as the last statement has been executed then all the variables that are created inside the definition are deleted.

So you must do:

a = sleepy(10,0.001)

print a

This will print the value of a which is the value that you returned from inside the function.

Also you can print z if you declare it as 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  ##notice this line has been changed.

        z = y - x
        t.append(z)
        print 'difference = ', z*1000

    print 'total:', (sum(t)/reps) * 1000

Now the value to be returned is in z and you can print it as so:

sleepy(10,0.001)

print z

Upvotes: 2

Douglas Leeder
Douglas Leeder

Reputation: 53310

z and snooze are local variables to the function.

You need to assign the result of the function to a variable to have it available after the function call.

Upvotes: 1

stackErr
stackErr

Reputation: 4170

When you return something from a function you are calling, the syntax is as follows:

p = sleepy(10,0.001)
print p

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121266

z is a local variable in your sleepy() function; it is not visible outside of that function.

Your function does return the value of z; assign it:

slept = sleepy(10, 0.001)
print slept

I used a different name here to illustrate that slept is a different variable.

Upvotes: 8

Related Questions