Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用一个函数的结果作为另一个函数的参数?_Python_Function_Variables_Arguments_Global - Fatal编程技术网

Python 如何使用一个函数的结果作为另一个函数的参数?

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

它有点像这样工作。这只是一段主代码<代码>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行中的错误是“未定义区域”
很抱歉问了一个愚蠢的问题,但我是新来的,我非常需要帮助

您正在尝试运行
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 1
    函数\u 2的类似参数。类似这样的内容:
    Function\u2(Function\u1(x,y))

@cᴏʟᴅsᴘᴇᴇᴅ 为什么你认为它不是代码?在你修复它之前它不是代码。@cᴏʟᴅsᴘᴇᴇᴅ 很抱歉打扰您,但您能帮我检查一下第32行上的情况吗?错误消息表明您正在使用分区,然后才将其分配给。