Ad

I would use a dictionary for this rather than adding multiple elif statements. It's easier to add new cases and is easier to read.

There's also no need to add items to a "grade" array if you use the dictionary approach as you can look up the key's (grade) value (score) straight away.

Removing the while loop means we don't need the y variable.

Applying the .upper() method to the input straight away makes it more readable in my oppinion.

Code
Diff
  • total = 0
    
    grade_dictionary = {
        "A+":4.0,
        "A":4.0,
        "A-":3.7,
        "B+":3.3,
        "B":3.0,
        "B-":2.7,
        "C+":2.3,
        "C":2.0,
        "C-": 1.7,
        "D": 1.0,
        }
    
    for _ in range(6):
        grade = input("Enter Your Grade For Each Class Listed in Order (Letter Form): ").upper()
        total += grade_dictionary.get(grade, 0)
        
    print(total / 6)
    • y = 0
    • while (y <=5):
    • grade = input("Enter Your Grade For Each Class Listed in Order (Letter Form): ")
    • grade = grade.upper()
    • grades.append(grade)
    • y = y + 1
    • calculate()
    • total = 0
    • def calculate():
    • total= 0
    • for element in grades:
    • if element == "A+":
    • total = total + 4.0
    • elif element == "A":
    • total = total + 4.0
    • elif element == "A-":
    • total = total + 3.7
    • elif element == "B+":
    • total = total + 3.3
    • elif element == "B":
    • total = total + 3.0
    • elif element == "B-":
    • total = total + 2.7
    • elif element == "C+":
    • total = total + 2.3
    • elif element == "C":
    • total = total + 2.0
    • elif element == "C-":
    • total = total + 1.7
    • elif element == "D":
    • total = total + 1.0
    • gpa = total / 6
    • print(gpa)
    • grade_dictionary = {
    • "A+":4.0,
    • "A":4.0,
    • "A-":3.7,
    • "B+":3.3,
    • "B":3.0,
    • "B-":2.7,
    • "C+":2.3,
    • "C":2.0,
    • "C-": 1.7,
    • "D": 1.0,
    • }
    • collect()
    • for _ in range(6):
    • grade = input("Enter Your Grade For Each Class Listed in Order (Letter Form): ").upper()
    • total += grade_dictionary.get(grade, 0)
    • print(total / 6)