Answer to Question #158546 in Python for Sara

Question #158546

Use the function written in the last lesson to calculate a student's GPA. Ask them how many classes they are taking, then ask them to enter the grades for


each class and if it is weighted.


Your program should then output the averaged GPA including the decimal


place.


Your main program must call the function.


Sample Run


HOw many classes are you taking? 7


Enter your Letter Grade: C

Is it weighted? (1 = yes) 1

Your GPA Score is: 3


Enter your Letter Grade: D

Is it weighted? (1 = yes) 0

Your GPA Score is: 1


Enter your Letter Grade: A

Is it weighted? (1 = yes) 1

Your GPA score is: 5


Enter your Letter Grade: B

Is it weighted? (1 = yes) 1

Your GPA Score is: 4


Enter your Letter Grade: C

Is it weighted? (1 = yes) 0

Your GPA Score is: 2


Your weighted GPA is a 3.0



1
Expert's answer
2021-01-26T08:10:30-0500
import sys

def GPAcalc(grade, response):       #fun to calculate gpa by taking Grade and response

    if response != 1 and response != 0:

        return sys.exit("Invalid response!")

    elif grade=="A" and response==1:

        return 5

    elif grade=="A" and response==0:

        return 4

    elif grade=="B" and response==1:

        return 4

    elif grade=="B" and response==0:

        return 3

    elif grade=="C" and response==1:

        return 3

    elif grade=="C" and response==0:

        return 2

    elif grade=="D" and response==1:

        return 2

    elif grade=="D" and response==0:

        return 1

    elif grade=="F" and response==1:

        return 1

    elif grade=="F" and response==0:

        return 0

    else:

        return sys.exit("Invalid Grade!")

def main():

    sum = 0

    num_classes = int(input("How many classes are you taking? "))   #ask for number of classes

    for x in range(num_classes):        #for each class take input

        grade = input("Enter your Letter Grade: ") #take input grade

        resp = int(input("Is it weighted? (1 = yes) "))

        print("Your GPA score is: %.2f" %GPAcalc(grade, resp)) #print gpa

        sum += GPAcalc(grade, resp)

    print("You weighted GPA is a %.2f" %(sum/num_classes)) #print averaged gpa

if __name__ == "__main__":

    main()

    input()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Sara
26.01.21, 20:39

Thanks!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS