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

时间:2018-09-29 02:23:31

标签: python python-3.x

代码很长,所以我不会输入。

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

我通过返回结果完成了函数。它叫return sudentp_file

现在我要继续将此新变量添加到另一个函数中。

因此,此新函数将获取Studentp_file的平均值。所以我做了一个新功能。会添加其他功能作为即时通讯的模板。

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

def mean(studentp_file):


mean()

让我感到困惑的是在mean()中添加了什么。我保留还是删除它?我知道你们不知道我正在使用的文件,但对函数和函数调用的工作原理有了一点基本的了解。谢谢。

4 个答案:

答案 0 :(得分:2)

调用函数时,您需要传递其所需的参数(基于您在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 :(得分:1)

我假设规范化函数在均值函数之前执行?如果是这样,请尝试以下结构:

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(...)的值。此函数调用-接受的参数(student_file, units_file)-将您的“ 原始”商品提供给函数normalise

return-基本上向代码中调用函数的点返回任何值。在这种情况下,将返回studentp_file的值返回到sp_filesp_file随后将获得studentp_file的值,然后可以将其传递给mean()函数。

/ ogs

答案 2 :(得分:0)

好吧,不清楚为什么不买(假的例子):

def f(a,b):
    return f2(3)+a+b
def f2(c):
    return c+1

f2中呼叫f,并在f2中返回

答案 3 :(得分:0)

如果始终将功能一的结果调用到功能二,则可以执行此操作。

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

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

print(f_one(1, 1))
2

或者只是一个想法...您可以设置一个变量z作为开关,如果它的1将结果传递给函数到下一个函数,或者{{1 }}返回函数一的结果

2
相关问题