![](/img/trans.png)
[英]how do I use return value from np.where or apply a function in it
[英]How do I extract a return value from a function and use the updated value?
我想从这个函数中获取返回值并为其分配一个变量,如果我输入“5”然后我们得到 15,但我想在下一个函数中继续使用更新后的变量。 我也想避免加入这些功能。
澄清一下,如果你输入这个函数,那么结果是 5+10 和 5+47。 我想要的答案是 5+10 然后是 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)
当前,您忽略了this_is_function
(即 15)的结果,只是将相同的值y
(即 5)再次传递给this_is_function2
。
将this_is_function
的结果保存在一个变量中,并将其传递给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
如果您不再需要原始y
,您还可以重用y
变量名称而不是使用新名称z
来作为中间结果。
y = this_is_function(y) # y is now 5 + 10 = 15 (no longer 5)
result = this_is_function2(y) # result is 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_function2(this_is_function(y))
您现在可以看到this_is_function2
的输入是this_is_function(y)
而this_is_function
的输入是y
因此,像我解决数学问题一样编写它 - 并且在调试时 python 执行此操作:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.