如何将返回的值传递给另一个函数

时间:2016-12-18 15:28:14

标签: python

我试图将我在子功能中创建的列表调用到另一个子功能中。使用参数和调用函数是我的跟腱,因为它与python有关。我想将我在calculateZscore函数中创建的newList调用到mu globalChi函数。

我目前的代码:

import os
import math

'''
c:/Scripts/Lab2Data
NCSIDS_ObsExp.txt
chisquare.txt
output.txt
'''
def main():
    directory = raw_input ("What is the working directory? ")    
    input_file = raw_input ("What is the name of the input file? ")    
    chiTable = raw_input ("What is the name of the chi-squared table? ")    
    outPut = raw_input ("What is the name of the output file? ")    
    path = os.path.join(directory,input_file)
    path_1 = os.path.join(directory, chiTable)
    path_2 =  os.path.join(directory, outPut)

if __name__ == "__main__":
   main()    

def calculateZscore(inFileName, outFileName):
    inputFile = open(inFileName,"r")  
    txtfile = open(outFileName, 'w')

for line in inputFile:
    newList = line.strip().split(',')
    obsExp = newList[-2:]
    obsExp = list(map(int, obsExp))
    obs = obsExp[0]
    exp = obsExp[1]
    zScore = (obs - exp) / math.sqrt(exp)
    zScore = map(str, [zScore])
    newList.extend(zScore)
    txtfile = open(outFileName, 'w')
    txtfile.writelines(newList) 

    inputFile.close() #close the files
    txtfile.close()
    return newList #return the list containing z scores

def globalChi(zscoreList):
print newList    

1 个答案:

答案 0 :(得分:1)

您应该阅读return声明。它用于使函数返回结果(与参数相反),并且不能在函数外部使用。 在这里,你正好相反。