我如何使用一个函数的结果作为另一个函数的参数?
How can i use the result of one function, as an argument of another function?
有点像这样。它只是一个主要代码块。 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 (zone)
所以第 32 行的错误是 "zone not defined"
抱歉问了一个愚蠢的问题,但我是新手,我非常需要帮助
您正在尝试 运行 Function_2
,但尚未调用 Function_1
。解决此问题的最简单方法是 运行 Function_2
对 Function_1
的结果,如下所示:
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))
当您调用 Function_1(1,2)
时,Function_1(1,2)
等于函数 return 编辑的内容,因此您只是设置 zone
这是 [=13] 的参数=] 到 Function_1(1,2)
的 return。
还有,你说的x,y是有定义的。您不能设置变量的值并将其与函数一起使用,您需要将其传入。例如:
x=1
def Function (x):
return x
Function()
这不起作用你需要传入x
当你这样调用它时:
def Function (x):
return x
Function(1)
这会将 x
设置为 1
看来您不太了解函数的工作原理,请尝试阅读 this 指南。
很正常!首先,你需要在使用它之前分配你的变量。 global zone
行允许 python 知道 zone
变量存在,而不是在函数中创建局部变量。如果单词 global
丢失,python 将创建另一个名称相同但地址不同的变量(本地)。
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 returns zone 的函数。
- 有必要使用
zone
作为全局变量吗?
- 您可以像 Function_2 的参数一样重复使用结果
Function_1
。像这样:Function_2 (Function_1 (x,y))
.
有点像这样。它只是一个主要代码块。 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 (zone)
所以第 32 行的错误是 "zone not defined" 抱歉问了一个愚蠢的问题,但我是新手,我非常需要帮助
您正在尝试 运行 Function_2
,但尚未调用 Function_1
。解决此问题的最简单方法是 运行 Function_2
对 Function_1
的结果,如下所示:
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))
当您调用 Function_1(1,2)
时,Function_1(1,2)
等于函数 return 编辑的内容,因此您只是设置 zone
这是 [=13] 的参数=] 到 Function_1(1,2)
的 return。
还有,你说的x,y是有定义的。您不能设置变量的值并将其与函数一起使用,您需要将其传入。例如:
x=1
def Function (x):
return x
Function()
这不起作用你需要传入x
当你这样调用它时:
def Function (x):
return x
Function(1)
这会将 x
设置为 1
看来您不太了解函数的工作原理,请尝试阅读 this 指南。
很正常!首先,你需要在使用它之前分配你的变量。 global zone
行允许 python 知道 zone
变量存在,而不是在函数中创建局部变量。如果单词 global
丢失,python 将创建另一个名称相同但地址不同的变量(本地)。
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 returns zone 的函数。
- 有必要使用
zone
作为全局变量吗? - 您可以像 Function_2 的参数一样重复使用结果
Function_1
。像这样:Function_2 (Function_1 (x,y))
.