2016-12-18 64 views
0

我想將我在子函數中創建的列表調用到另一個子函數中。使用參數和調用函數是我的致命弱點,因爲它涉及到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  
+0

你能否創建一個更簡單的例子來說明你正在做什麼? –

+0

另外,您的縮進在您在此處發佈的代碼中顯示爲關閉。請將其編輯爲與您實際運行的相同。請注意,對於本網站的正確格式,除了實際縮進的空格外,還必須在每行的開頭添加四個空格。 –

回答

1

你應該閱讀有關return聲明。它用於使函數返回結果(與參數相反),並且不能在函數外部使用。 在這裏,你做的是完全相反的。

相關問題