Python Forum
Problem Calculating GPA
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem Calculating GPA
#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


Messages In This Thread
Problem Calculating GPA - by FirstBornAlbratross - Apr-12-2023, 08:19 PM
RE: Problem Calculating GPA - by ibreeden - Apr-13-2023, 10:06 AM
RE: Problem Calculating GPA - by deanhystad - Apr-13-2023, 01:47 PM

Forum Jump:

User Panel Messages

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