Grade to GPA Conversion

Write a program that takes a grade (A/B/C/D/F) as input and returns the corresponding GPA (4/3/2/1/0).

Example 1:

Input: "A"

Output: 4

Example 2:

Input: "C"

Output: 2

Use a dictionary to map the grades to their respective GPA scores.

def grade_to_gpa(grade):
    gpa_dict = {"A": 4, "B": 3, "C": 2, "D": 1, "F": 0}
    return gpa_dict.get(grade, "Invalid grade")

© Let’s Data Science


Unlock AI & Data Science treasures. Log in!