Python Forum
Problem Calculating GPA
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Calculating GPA
#1
Hey all, I have created a simple program to try to convert letter grades into a GPA score, but my program calculations are a bit off.

user_grades = []

grades = str(input("Enter a grade: ('q' to end.)"))
user_grades.append(grades)

while grades != 'q':
    grades = str(input("Enter a grade: ('q' to end.)"))
    user_grades.append(grades)
else:
    user_grades.pop()
    print(f"{user_grades}")
    
total = 0

for grade in user_grades:
    if "A+" in user_grades:
        total += 4.0
    elif "A" in user_grades:
        total += 4.0
    elif "A-" in user_grades:
        total += 3.7
    elif "B+" in user_grades:
        total += 3.3
    elif "B" in user_grades:
        total += 3
    elif "B-" in user_grades:
        total += 2.7
    elif "C+" in user_grades:
        total += 2.3
    elif "C" in user_grades:
        total += 2.0
    elif "C-" in user_grades:
        total += 1.7
    elif "D+" in user_grades:
        total += 1.3
    elif "D" in user_grades:
        total += 1.0
    elif "F" in user_grades:
        total += 0
    print(f"{total}")
Any help would be greatly appreciated.

Thanks!
Larz60+ write Apr-12-2023, 09:51 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
I have fixed this for you. Please use BBCode tags on future posts.
Reply
#2
(Apr-12-2023, 08:19 PM)FirstBornAlbratross Wrote: but my program calculations are a bit off.
Yes indeed. You take each item of the list and call it "grade".
for grade in user_grades:
But then you compare the literal value with the whole list "user_grades":
    if "A+" in user_grades:
You should instead compare with the one "grade" item. Like this:
for grade in user_grades:
    if "A+" in grade:
        total += 4.0
    elif "A" in grade:
        total += 4.0
    elif "A-" in grade:
        total += 3.7
    ...
And there is one more problem. Let us say the grade is "A-". You first test if "A+" is in "A-". It is not, so you continue with testing if "A" is in "A-". There is! So the next elif "A-" is in "A-" will never be executed.
So you must first test for "A+" and "A-" and then for "A". Another method is to use the "==" operator instead of "in".

Does this help you?


One more detail: the result of input() is a string. So you need not convert this result to a sting.
grades = str(input("Enter a grade: ('q' to end.)"))
It is sufficient to do:
grades = input("Enter a grade: ('q' to end.)")
And the last thing. You did solve the input part, but need some duplicate lines. You might also start with an infinite loop and exit this loop when "q" is entered. I think that looks nicer, but you are free to use it or not.
user_grades = []
while True:
    grade = input("Enter a grade: ('q' to end.) ")
    if grade == "q":
        break   # Stop asking grades.
    else:
        user_grades.append(grade)
Reply
#3
As iberdeen says, your loop is not looping through user_grades and converting them to number grades. Instead you are looking up the highest grade every time through the loop. Essentially this:
for grade in grades:
    Did anyone get an A+?
        total += 4
    Did anyone get an A?
        total += 4
    Did anyone get an A-?
        total += 3.7
    ...
When you should be doing:
for grade in grades:
    if grade == A+?
        total += 4
    else if grade == A?
        total += 4
    else if grade == A-?
        total += 3.7
    ...
Does the assignment require you to use two loops? One will do.
Output:
grades = [] while True: letter_grade = input("Enter a grade: ('q' to end.)").upper() if letter_grade == "Q": break elif letter_grade in ("A+", "A"): grades.append(4) elif letter_grade == "A-": grades.append(3.7) .... average = sum(grades) / len(grades)
Finally, using a bunch of if statements is not "pythonic". I would use a dictionary lookup. I would make a dictionary that converts letter grades to number grades:
letter_grades = {
    "A+": 4, "A": 4, "A-": 3.7, "B+": 3.3, "B": 3, "B-": 2.7,
    "C+": 2.3, "C": 2, "C-": 1.7, "D+": 1.3, "D": 1, "F": 0
}
Then the conversion is simple and short:
number_grade = letter_grades[user_input]
My program to take in letter grades and compute an average is 17 lines long with input checking and nice formatting. Your if statement is 23 lines long.
Reply
#4
Thank you guys for the quick responses!

They really helped!

Also, the chapter I'm on hasn't introduced dictionaries (yet). I know how to use them but I opted to try and solve the exercise without it.

Really appreciate the input guys,

Many thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020