Use return value of a function

I started programing recently, and I started with python. My question is: how can I use a result of a function in an another function ?

def eklid(p, a, b,):
    x = [1, 0]
    y = [0, 1]
    r = [a, b]
    q = [0]
    n = 0
    while r[n+1] != 0:
        q.append(r[n] // r[n+1])
        r.append(r[n] % r[n+1])
        x.append(x[n] - x[n+1] * q[n+1])
        y.append(y[n] - y[n+1] * q[n+1])

        if p == 0:
            print(r[n], "=", r[n+1], "*", q[n+1], "+", r[n+2])
        elif p == 1:    # extended print
            print(r[n+2], "t", x[n+2], "t", y[n+2], "t", r[n+2], "=", a, "*", x[n+2], "+", b, "*", y[n+2])
        elif p == -1:
            k =1
        else:
            print("wrong input")
        n += 1
    return x, y, r, q, n,

I have this function eklid() and I want to use x and r from it in this function:

def cong_solv(x, r, b,):
    result = x/r
    int_result = int(result)
    return int_result

How can I do that?


# Here, a=x, b=y, c=r, d=q, e=n
a, b, c, d, e = eklid(h, i, k)

# Assuming based on your function definitions you want the
# same value as the third argument
final_result = cong_solv(a, c, k)

You get the return values from eklid and save them into variables. You then use those variables to call the next function.

Of course, in a real code you should name your varialbes better than I did in this example. I deliberately did not call the variables the same names as inside the function to demonstrate that you don't have to.


One way would be to call the eklid() function from inside the cong_solv() function. Something like this should work:

def cong_solv(x, r, b):
   p = "foo"
   b = "bar"
   x, y, r, q, n = eklid(p, a, b)

   result = x/r 
   int_result = int(result) 
   return int_result

In python, when you return more than one variable, it returns a tuple. You can retrieve the value by its index (returned_value[0], returned_value[1]) or unpack the tuple like Mike Driscoll said (a, b, c, d = eklid(h, i, k)).

Since I got two downvotes, I am going to give you better (I hope) explanation: Everytime you return more than one value, it returns a tuple.

def my_function():
   a = 10
   b = 20
   return a, b

print type(my_function()) # <type 'tuple'>

But if you return just one value:

def my_function():
    a = 10
    return a

print type(my_function()) # <type 'int'>

So if you want to use your value, you can:

Unpack tuple values like this

a, b = my_function()

This way you get your return values in the same order you return inside my_function.

Rewriting your code, you can simply do:

a, b, c = eklid(10, 20, 30) # it will return a tuple

And call your other function:

cong_solv(a, b, 20)

In my honest opinion I would return a dict. With dict you can be explicit because your values have key names.

Inside your eklid return function:

return d # d = {"what_x_means": x,
         #      "what_y_means": y,
         #      "what_r_means": r,
         #      "what_q_means": q, 
         #      "what_n_means": n}

And retrieve for its key:

d["what_x_means"]
d["what_r_means"]
链接地址: http://www.djcxy.com/p/53140.html

上一篇: Python暴露于范围

下一篇: 使用函数的返回值