简体   繁体   中英

How do I extract a return value from a function and use the updated value?

I would like to get the return value from this function and assign it a variable if I put in "5" then we get 15 out but I want to keep using the updated variable afterwards in the next function. I would like to avoid joining the functions too.

To clarify, if you input this function then the result is 5+10 and 5+47. What I want as an answer is 5+10 and then 15+47.

y = input("here you input your number: ") 

def this_is_function(x):
    return int(x) + 10

def this_is_function2(x):
    return int(x) + 47

this_is_function(y)
this_is_function2(y)

Currently you are ignoring the result of this_is_function (which is 15) and are just passing the same value y (which is 5) again to this_is_function2 .

Save the result of this_is_function in a variable instead, and pass it to this_is_function2 :

z = this_is_function(y)        # z is 5 + 10 = 15 (y is still 5)
result = this_is_function2(z)  # result is 15 + 47

If you don't need the original y anymore, you can also reuse the y variable name instead of using a new name, z , for the intermediate result.

y = this_is_function(y)        # y is now 5 + 10 = 15 (no longer 5)
result = this_is_function2(y)  # result is 15 + 47

If you are wanting the input to the second function (equation) to be the output of the first, then you can do just that:

y = input("here you input your number: ") 

def this_is_function(x):
    return int(x) + 10

def this_is_function2(x):
    return int(x) + 47

^leaving all this the same

Then using this:

this_is_function2(this_is_function(y))

You can now see that the input to this_is_function2 is this_is_function(y) and the input to this_is_function is y

So writing it as I would solve a math problem - and as python performs this when debugging:

this_is_funtion2(this_is_function(y))    # Original line
this_is_funtion2(this_is_function(5))    # y = 5
this_is_funtion2(15)                     # this_is_function(5) = 15
62                                       # this_is_function2(15) = 62

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM