2013-06-30 54 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

相关问题