Python 如何使用一个函数的结果作为另一个函数的参数?
它有点像这样工作。这只是一段主代码<代码>x,Python 如何使用一个函数的结果作为另一个函数的参数?,python,function,variables,arguments,global,Python,Function,Variables,Arguments,Global,它有点像这样工作。这只是一段主代码x,y已正确定义 def Function_1 (x,y): global zone # there are some conditions which get the value of variable zone return zone def Function_2 (zone): #then there are some conditions wherein i use variable zone Function_2
y
已正确定义
def Function_1 (x,y):
global zone
# there are some conditions which get the value of variable zone
return zone
def Function_2 (zone):
#then there are some conditions wherein i use variable zone
Function_2 (zone)
因此,第32行中的错误是“未定义区域”
很抱歉问了一个愚蠢的问题,但我是新来的,我非常需要帮助您正在尝试运行
Function\u 2
,而Function\u 1
尚未调用。解决此问题的最简单方法是对函数_1
的结果运行函数_2
,如下所示:
def Function_1 (x,y):
global zone
#there are some conditions which get the value of variable zone
return zone
def Function_2 (zone):
#then there are some conditions wherein i use variable zone
Function_2(Function_1(1,2))
def Function (x):
return x
Function(1)
当您调用函数_1(1,2)
时,函数_1(1,2)
将等于函数返回的值,因此您只需将区域
设置为函数_1(1,2)
的参数
还有,你说x和y是有定义的。您不能设置变量的值并将其与函数一起使用,您需要传入它。例如:
x=1
def Function (x):
return x
Function()
这不起作用,当您这样调用它时,需要传入x
:
def Function_1 (x,y):
global zone
#there are some conditions which get the value of variable zone
return zone
def Function_2 (zone):
#then there are some conditions wherein i use variable zone
Function_2(Function_1(1,2))
def Function (x):
return x
Function(1)
这会将x
设置为1
您似乎对函数的工作原理不太了解,请尝试阅读指南。这很正常!首先,您需要在使用变量之前分配它。行
global zone
允许python知道zone
变量存在,并且没有在函数中创建局部变量。如果缺少单词global
,python将创建另一个名称相同但地址不同的变量(local)
zone = None
def Function_1 (x,y):
global zone
#there are some conditions which get the value of variable zone
return zone
def Function_2 (zone):
#then there are some conditions wherein i use variable zone
Function_2 (zone)
在查看代码时,我们看到名为function_1的函数返回区域
- 是否需要将
用作全局变量区域
- 您可以重复使用结果
函数\u 2的类似参数。类似这样的内容:函数\u 1
Function\u2(Function\u1(x,y))