Python 在另一个函数中调用函数的结果

Python 在另一个函数中调用函数的结果,python,python-3.x,Python,Python 3.x,代码很长,所以我不会键入它 作为一个初学者,我感到困惑的是函数调用。所以我有一个csv文件,函数将所有内容(它们是整数)除以95,得到标准化分数 我通过返回结果完成了函数。它被称为返回sudentp\u文件 现在我想把这个新变量继续到另一个函数中 因此,这个新函数将获得studentp_文件的平均值。所以我做了一个新函数。我将添加另一个函数作为我所做工作的模板 def normalise(student_file, units_file) ~ Do stuff here ~ return stu

代码很长,所以我不会键入它

作为一个初学者,我感到困惑的是函数调用。所以我有一个csv文件,函数将所有内容(它们是整数)除以95,得到标准化分数

我通过返回结果完成了函数。它被称为
返回sudentp\u文件

现在我想把这个新变量继续到另一个函数中

因此,这个新函数将获得studentp_文件的平均值。所以我做了一个新函数。我将添加另一个函数作为我所做工作的模板

def normalise(student_file, units_file)
~ Do stuff here ~
return studentp_file

def mean(studentp_file):


mean()

我感到困惑的是,应该在平均值中加什么()。我是保留它还是移除它?我知道你们不知道我正在使用的文件,如果能对函数和函数调用的工作原理有一点基本的了解,我们将不胜感激。谢谢。

嗯,不清楚为什么不买(虚拟示例):


调用
f
中的
f2
,并返回
f2

我假设在均值函数之前执行归一化函数?如果是,请尝试这种结构:

def normalise(student_file, units_file):
    #do stuff here
    return studentp_file

def mean(studentp_file):
    #do stuff here


sp_file = normalise(student_file, units_file)
mean(sp_file)
python(2/3)中的函数是为了可重用性而设计的,并使代码保持在一个块中。根据您传递的参数(如果它接受参数),这些函数可能返回值,也可能不返回值。想象一下,功能就像生产成品的现实工厂。原材料被送入工厂,以便生产出成品。函数也是这样的。:)

现在,请注意,我为一个名为
sp_file
的变量分配了函数调用
normalise(…)
的值。此函数调用-接受的参数
(学生文件、单位文件)
-这些是您的“原始”货物,将被送入您的函数
正常化

return
-基本上返回代码中调用函数的点的任何值。在本例中,return将
studentp\u file
的值返回到
sp\u file
sp_文件
然后将获得
studentp_文件
的值,然后可以传递给
mean()
函数


/ogs

调用函数时,需要传入函数所需的参数(基于您在
def
语句中指定的参数。因此,您的代码可能如下所示:

def normalise(student_file, units_file)
~ Do stuff here ~
    return studentp_file

def mean(studentp_file):
~ other stuff here ~
     return mean


# main code starts here

# get student file and units file from somewhere, I'll call them files A and B. Get the resulting studentp file back from the function call and store it in variable C.

C = normalize(A, B)

# now call the mean function using the file we got back from normalize and capture the result in variable my_mean

my_mean = mean(C) 

print(my_mean)

如果函数1的结果总是被函数2调用,那么您可以这样做

def f_one(x, y):
    return (f_two(x, y))

def f_two(x, y):
    return x + y

print(f_one(1, 1))
或者只是一个想法……您可以设置一个变量
z
,如果它的
1
将结果传递给下一个函数,或者如果
2
返回函数1的结果,则该变量可用作开关

def f_one(x, y, z):
    result = x + y
    if z == 1:
        return (f_two(result))
    elif z == 2:
        return result

def f_two(x):
    return x - 1

a = f_one(1, 1, 1)
print(a)
b = f_one(1, 1, 2)
print(b)

调用
normalise()
时,获取一个变量的返回值,然后将该变量传递给
mean()
函数。我想勾选您的响应,但两个响应都很好,只能勾选一个。谢谢您的回答。
2
def f_one(x, y, z):
    result = x + y
    if z == 1:
        return (f_two(result))
    elif z == 2:
        return result

def f_two(x):
    return x - 1

a = f_one(1, 1, 1)
print(a)
b = f_one(1, 1, 2)
print(b)