Start learning free

LESSON 200 OF 287 · PROJECTS: EVERYDAY PROGRAMS

Report Card V4 in Python: Grades for a Whole Class

Practice Read the full explanation

Combine lists, loops, and conditionals to print a letter grade for each student in a class.

You start with:

names = ["Alice", "Bob", "Carol", "Dan"]
scores = [92, 78, 85, 67]

Build:

Alice: A
Bob: C
Carol: B
Dan: D

Rules:

  • Loop over the indices of the students (range(len(names))) so you can read the name and the score at the same position.
  • Use the same scale as V3: 90+ A, 80-89 B, 70-79 C, 60-69 D, below 60 F.
  • On each iteration print the name, a colon and a space, then the grade, for example Alice: A.

Inside for i in range(len(names)):, read scores[i] and apply the V3 grading chain.

names = ["Alice", "Bob", "Carol", "Dan"]
scores = [92, 78, 85, 67]

for i in range(len(names)):
    score = scores[i]
    if score >= 90:
        grade = "A"
    elif score >= 80:
        grade = "B"
    elif score >= 70:
        grade = "C"
    elif score >= 60:
        grade = "D"
    else:
        grade = "F"
    print(names[i] + ": " + grade)

STEP 1 OF 8
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Report Card V4 in Python, explained

    Problem overview

    Version 4 grades a whole class instead of one score. Two lists are given: names holds the students and scores holds their marks, and position i in both lists belongs to the same student. For each student the program prints the name, a colon, a space and the letter grade, using the scale from version 3: 90 and above is an A, 80 to 89 a B, 70 to 79 a C, 60 to 69 a D and below 60 an F.

    With the starter data the output is:

    Alice: A
    Bob: C
    Carol: B
    Dan: D

    This time the program prints the lines itself, one per student.

    Approach: loop over positions, grade each score

    The grading ladder from version 3 stays as it is. What changes is that it now runs once per student, so it moves inside a loop. The loop needs the name and the score from the same position, so it counts positions, 0 to the last one, and uses each position to read both lists.

    range(len(names)) produces exactly those positions: 0, 1, 2, 3 for four students. At each position, the score comes from scores[i] and the name from names[i].

    Step by step

    1. Write the loop for i in range(len(names)):. Everything that follows is indented inside it.
    2. Read the score into score = scores[i]. This gives the ladder a short name to test.
    3. Start the ladder with if score >= 90: and set grade = "A". The highest grade comes first, so each later test needs only a lower limit.
    4. Add elif score >= 80: for "B".
    5. Add elif score >= 70: for "C".
    6. Add elif score >= 60: for "D".
    7. End with else: for "F". Now grade gets a value on every pass.
    8. Print the line after the ladder, still inside the loop, so it runs once per student: the name, ": " and grade.

    The solution in Python

    names = ["Alice", "Bob", "Carol", "Dan"]
    scores = [92, 78, 85, 67]
    
    for i in range(len(names)):
        score = scores[i]
        if score >= 90:
            grade = "A"
        elif score >= 80:
            grade = "B"
        elif score >= 70:
            grade = "C"
        elif score >= 60:
            grade = "D"
        else:
            grade = "F"
        print(names[i] + ": " + grade)
    

    A few details about this code:

    • Both parts of the line are strings. names[i], ": " and grade are all text, so + joins them. A number would have to go through str() first.
    • The indentation decides what repeats. The print is indented at the same level as the if, so it runs once per student. If it were moved out to the left margin, it would run once, after the loop, and print only the last student's line.
    • Variables carry over between passes. score and grade are overwritten on every pass. That is safe here because the ladder always assigns grade.

    Tracing the first two students

    On the first pass, i is 0, so score becomes 92. The test 92 >= 90 is true, grade becomes "A", and the program prints Alice: A. The remaining branches are skipped.

    On the second pass, i is 1 and score is 78. The first two tests fail, because 78 is below 90 and below 80. The test 78 >= 70 succeeds, so grade becomes "C" and the program prints Bob: C. Carol (85) lands in the B branch and Dan (67) in the D branch in the same way.

    Looping over positions or over the lists directly

    The lesson counts positions because one number, i, can then read both lists. That is a pattern worth knowing, since it works whenever several lists are parallel. The scores are a plain list of numbers, so you can't loop over them and also learn whose score each is. Looping over names alone has the opposite problem.

    Python also offers zip, which walks several lists side by side: for name, score in zip(names, scores): gives each name together with its score, and it stops at the shorter list instead of raising an error. The lesson's steps use range(len(names)), and the solution above is written that way.

    Edge cases and common mistakes

    • Lists of different lengths. The loop counts positions of names. If scores were shorter, for instance one score for two names, the first student would print normally and the second would stop the program with an IndexError: list index out of range. If scores were longer, the extra scores would be ignored without any message.
    • Boundary scores. Scores of 90, 59 and 60 give A, F and D, because each limit uses >=.
    • An empty class. With empty lists, range(0) has no positions, so the loop body never runs and nothing is printed.
    • Leaving out the else. Without it, a failing score would not assign grade at all. On the first pass that raises a NameError, but on a later pass the variable still holds the previous student's grade, so a failing student would be printed with someone else's letter and no error at all. Ending the ladder with else removes the problem.
    • Printing outside the loop. This is the indentation mistake described above.

    Testing

    Another class worth trying is names = ["Eve", "Finn", "Gus"] with scores = [90, 59, 60]. It prints:

    Eve: A
    Finn: F
    Gus: D
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free