indian_trash
indian_trash

Reputation: 1

Calculating GPA with function in Python

Ok so I am trying to make a program where the user has to input a letter grade for a course along with the course credit. There are three courses overall. It adds up all of the grade points for each course (grade point * course credit) and divides by the total amount of credits for the three courses.

So I typed this in for the input variables and it got this all right:

Enter your course 1 letter grade: A
Enter your course 1 credit: 3
Grade point for course 1 is: 4.0
Enter your course 2 letter grade: B
Enter your course 2 credit: 2
Grade point for course 2 is: 3.0
Enter your course 3 letter grade: C
Enter your course 3 credit: 4
Grade point for course 3 is: 2.0

The overall GPA should be 2.8888888888889, but my program prints out 18.88888888888889. Can someone tell me what I'm doing wrong here?

Here is my code:

def getGradePoint(courseLetterGrade):
  if (courseLetterGrade == "A"):
    return 4.0;
  elif (courseLetterGrade == "A-"):
    return 3.67;
  elif (courseLetterGrade == "B+"):
    return 3.33;
  elif (courseLetterGrade == "B"):
    return 3.0;
  elif (courseLetterGrade == "B-"):
    return 2.67;
  elif (courseLetterGrade == "C+"):
    return 2.33;
  elif (courseLetterGrade == "C"):
    return 2.0;
  elif (courseLetterGrade == "D"):
    return 1.0;
  else:
    return 0.0;
  
# Main area of code - Code that allows the user to input the variables and then calls to the getGradePoint function
def run():  
  courseLetterGrade = str(input("Enter your course 1 letter grade: "))
  courseCredit = float(input("Enter your course 1 credit: "))
  print(f"Grade point for course 1 is {getGradePoint(courseLetterGrade)}.")
  
  courseLetterGrade2 = str(input("Enter your course 2 letter grade: "))
  courseCredit2 = float(input("Enter your course 2 credit: "))
  print(f"Grade point for course 2 is {getGradePoint(courseLetterGrade2)}.") 
  
  courseLetterGrade3 = str(input("Enter your course 3 letter grade: "))
  courseCredit3 = float(input("Enter your course 3 credit: "))
  print(f"Grade point for course 3 is {getGradePoint(courseLetterGrade3)}.")

  GPA = (getGradePoint(courseLetterGrade) * courseCredit) + (getGradePoint(courseLetterGrade2) * courseCredit2) + (getGradePoint(courseLetterGrade3) * courseCredit3) / (courseCredit + courseCredit2 + courseCredit3)

  print(f"Your GPA is: {str(GPA)}")

if __name__ == "__main__":
  run()

Upvotes: 0

Views: 4091

Answers (2)

Belle Zaid
Belle Zaid

Reputation: 182

You should use this for your calculation

GPA = ((getGradePoint(courseLetterGrade) * courseCredit) + (getGradePoint(courseLetterGrade2) * courseCredit2) + (getGradePoint(courseLetterGrade3) * courseCredit3)) / (courseCredit + courseCredit2 + courseCredit3)

Following the rule of the Order of Operations PEMDAS. As the / was used first without the brackets. . For reference. https://www.mathsisfun.com/operation-order-pemdas.html

Upvotes: 0

tomocafe
tomocafe

Reputation: 1564

Mind your order of operations.

What you're getting is:

(4*3) + (3*2) + ((2*4)/(3+2+4)) = 18.88...

but what you wanted was

((4*3) + (3*2) + (2*4)) / (3+2+4) = 2.88...

The division operator / has higher precedence than + so without any parentheses indicating to do all the addition first before dividing, it will do the division first with the last weighted grade, then add the other weighted grades.

More on operator precedence: 6.17. Operator precedence

How do order of operations go on Python?

Upvotes: 4

Related Questions