2013-06-30 56 views
2

在下面的GPA計算程序中,我的兩個函數採用相同的參數。將相同參數傳遞給兩個或多個函數的最佳方式是什麼?謝謝!將相同的參數傳遞給多個函數 - Python

def main(): 
    cumulative(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4) 
    print "Your semester 5 gpa is:", sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4)[0] 

def cumulative(ee311, cpre281, math207, ee332, jlmc101): 
    qpts_so_far = 52 + 40.97 + 47.71 + 49 
    total_qpts = qpts_so_far + sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4)[1] 
    total_gpa = total_qpts/(13 + 13 + 13 + 15 + 17) 
    print "Your cumulative GPA is:", total_gpa 

def sem_5(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4): 
    sem_5_qpts = 4*ee311 + 4*cpre281 + 3*math207 + 3*ee332 + 3*jlmc101 
    sem_5_gpa = (sem_5_qpts)/17.0 
    return sem_5_gpa, sem_5_qpts 

if __name__ == '__main__': 
    main() 
+0

FYI:你'cumulative'功能從來沒有使用傳遞給它的參數。你有意將它們傳遞給'sem_5'嗎?如果是這樣,你可以更容易地將它改爲'def cumulative(** kwargs)',然後調用'+ sem_5(** kwargs)' –

回答

4

你可以通過相同的單詞每個使用**(見here):

args = dict(ee311=3, cpre281=3, math207=3.67, ee332=3, jlmc101=4) 
cumulative(**args) 
print "Your semester 5 gpa is:", sem_5(**args)[0] 
+0

對於args,單個元組/列表的開頭可以正常工作這裏也。 – DaoWen

相關問題