3

在下面的 GPA 计算程序中,我的两个函数采用相同的参数。将相同参数传递给两个或多个函数的最佳方法是什么?谢谢!

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()
4

1 回答 1

8

您可以将相同的字典传递给每个使用**(请参见此处):

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]
于 2013-06-30T23:09:06.620 回答