8

In the GPA calculating program below, my two functions take the same parameters. What is the best way to pass the same parameters to two or more functions? Thank you!

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()
1
  • FYI: Your cumulative function never uses the arguments passed to it. Did you mean to pass them on to sem_5? If so, you'd have an easier time changing it to def cumulative(**kwargs), and then call + sem_5(**kwargs) Commented Jun 30, 2013 at 23:34

1 Answer 1

14

You could pass the same dictionary to each using ** (see 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]
Sign up to request clarification or add additional context in comments.

1 Comment

A single start with a tuple/list for the args would work fine here too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.