Start learning free

LESSON 240 OF 673 · HELPER FUNCTIONS

Letter Grades in Python: A Helper Function and a Loop

Practice Read the full explanation

Given a list of integers scores, return the letter grade of every score, in the same order as the scores.

Each score gets one letter:

  • 90 to 100: "A"
  • 80 to 89: "B"
  • 70 to 79: "C"
  • 60 to 69: "D"
  • 0 to 59: "F"

The work is split between two functions. The first one is a helper: it gets one score and returns its letter. The second one, the function that is graded, goes over the scores and collects the helper's letter for each of them.

 

Example 1:

Input: scores = [95, 82, 71]
Output: ["A", "B", "C"]
Explanation: 95 is in the A range (90 to 100), 82 is in the B range and 71 is in the C range.

Example 2:

Input: scores = [65, 40]
Output: ["D", "F"]
Explanation: 65 is between 60 and 69, so it gets a D. 40 is below 60, so it gets an F.

Example 3:

Input: scores = [100, 90, 89]
Output: ["A", "A", "B"]
Explanation: 100 and 90 are both in the A range: 90 is its lowest score. 89 is one point short of it, so it gets a B.

 

Constraints:

  • scores has at least one score.
  • Every score is an integer from 0 to 100.
  • The result has one letter per score, in the same order as scores.
STEP 1 OF 9
    Ctrl + Enter to check

    PYTHON · HELPER FUNCTIONS

    Letter Grades in Python, explained

    Problem overview

    Given a list of integer scores from 0 to 100, return the letter grade of each score, in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and 0 to 59 for "F".

    The work is split in two. letterGrade is a helper that turns one score into its letter. letterGrades goes over the list and collects the helper's answer for every score. For [95, 82, 71] the result is ["A", "B", "C"], and for [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short.

    Approach

    The helper checks the ranges from the top down. A score of at least 90 is an A. If it is not, it is below 90, so a score of at least 80 must be a B, and so on. Each check can lean on the ones before it, so it only needs a lower bound, never a pair of bounds like 80 <= score <= 89.

    The main method is the usual collect-in-a-list pattern: start with an empty list, visit each score, append its letter, return the list.

    Step by step

    1. Check for an A first: if score >= 90, return "A". Because return ends the function, the checks below only ever see scores under 90.
    2. Check for a B: score >= 80 now means 80 to 89.
    3. Check for a C: score >= 70 means 70 to 79.
    4. Check for a D: score >= 60 means 60 to 69.
    5. Return "F" after the last if. Only scores below 60 get here, so no test and no else is needed.
    6. In letterGrades, start an empty list named grades.
    7. Loop over scores, one score at a time.
    8. Append self.letterGrade(score) to grades. The helper's parameter is also called score, but each function has its own variables: the call hands the helper this loop's value.
    9. Return grades after the loop. Returning inside the loop would send back only the first letter.

    The solution in Python

    class Solution:
        def letterGrade(self, score: int) -> str:
            if score >= 90:
                return "A"
            if score >= 80:
                return "B"
            if score >= 70:
                return "C"
            if score >= 60:
                return "D"
            return "F"
    
        def letterGrades(self, scores: List[int]) -> List[str]:
            grades = []
            for score in scores:
                grades.append(self.letterGrade(score))
            return grades
    

    Python details:

    • Separate if statements work here because each one returns. There is no fall-through, so elif would behave the same. The chain of if lines is just as correct and easy to read.
    • self. before the helper's name is how one method of the class calls another.
    • append changes the list in place and returns None. Don't write grades = grades.append(...): that replaces the list with None.

    Following one score through the checks

    Take a score of 82. The test score >= 90 is false, so the function moves on. The test score >= 80 is true, so it returns "B" and the remaining tests never run. A score of 40 fails all four tests and reaches the final return "F". A score of 90 passes the very first test, which is why 90 is an A and 89 is a B: 89 fails that test and passes the next one.

    Why the helper is a separate method

    The range checks and the looping are two different jobs. Keeping letterGrade separate means it can be read, tested and fixed by itself: letterGrade(95) returns "A", letterGrade(82) returns "B", and letterGrade(40) returns "F". The list method then has no grading rules in it, only the loop. If the cut-offs change, only the helper changes.

    The loop could also be written as a list comprehension, [self.letterGrade(s) for s in scores], which returns the same list. The explicit loop with append is the form the lesson practises: start empty, visit each item, add one result per item.

    An empty list of scores would give an empty list back, because the loop body never runs. The lesson guarantees at least one score, but the code handles the empty case anyway.

    Edge cases and common mistakes

    • Wrong order of checks. If the first test were score >= 60, a 95 would get a D. The first test that is true wins, and 95 is at least 60. The highest range must go first when each test has only a lower bound.
    • Boundary scores. 90, 80, 70 and 60 belong to the higher grade, so the tests use >=, not >. With > a 90 would become a B.
    • Returning inside the loop. Only one letter would come back, not one per score.
    • Order. The result has one letter per score in the input's order, including repeated scores.

    Testing

    • [95, 82, 71] gives ['A', 'B', 'C'].
    • [65, 40] gives ['D', 'F'].
    • [100, 90, 89] gives ['A', 'A', 'B'].
    • [0, 59, 60, 69, 70, 79, 80, 89] gives ['F', 'F', 'D', 'D', 'C', 'C', 'B', 'B']: every boundary lands in the expected range.

    Python prints strings in a list with single quotes; the values are the same strings as "A" and "B".

    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free