Start learning free

LESSON 200 OF 673 · PROJECTS: EVERYDAY PROGRAMS

Report Card V3 in Python: Letter Grade with if/elif/else

Practice Read the full explanation

Use if/elif/else to turn a numeric score into a letter grade.

You start with:

score = 85

Build:

Grade: B

Rules:

  • score >= 90 is "A"; 80-89 is "B"; 70-79 is "C"; 60-69 is "D"; below 60 is "F".
  • The boundaries count: a score of exactly 90 is an A, exactly 80 a B, exactly 70 a C, exactly 60 a D, and 59 is an F.
  • Store the result in a variable named grade. The checker prints "Grade: " followed by its value for you, so do not print anything yourself.

Use a chain: if score >= 90: ... elif score >= 80: ... else: ....

score = 85

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · PROJECTS: EVERYDAY PROGRAMS

    Report Card V3 in Python, explained

    Problem overview

    Version 3 turns a numeric score into a letter grade. The scale is 90 and above for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D" and anything below 60 for "F". The boundaries belong to the higher grade: exactly 90 is an A, exactly 60 is a D, and 59 is an F.

    With the starter value score = 85 the grade is "B", and the expected output reads Grade: B. Store the letter in a variable named grade. The lesson's tests print the label, so your code prints nothing itself.

    Approach: a ladder from the top grade down

    A chain of if, elif and else tests its conditions from top to bottom and runs only the first branch whose condition is true. That order is the key idea. If the chain starts with the highest grade, then by the time Python reaches a later branch it already knows every earlier condition was false, so each branch needs only a lower limit.

    For a score of 85, the first test (score >= 90) fails, and the second (score >= 80) succeeds. Python sets grade = "B" and skips the rest. It doesn't matter that 85 is also at least 70 and at least 60: those branches are never examined.

    The final else has no condition. It catches everything that fell through all the tests, which is every score below 60.

    Step by step

    1. Start with the top grade. if score >= 90: sets grade = "A".
    2. Add elif score >= 80: for "B". Only scores below 90 reach this test, so it means 80 to 89.
    3. Add elif score >= 70: for "C", which means 70 to 79.
    4. Add elif score >= 60: for "D", which means 60 to 69.
    5. Finish with else: for "F". Everything below 60 is what is left, so no test is needed.

    The solution in Python

    score = 85
    
    if score >= 90:
        grade = "A"
    elif score >= 80:
        grade = "B"
    elif score >= 70:
        grade = "C"
    elif score >= 60:
        grade = "D"
    else:
        grade = "F"
    

    Some details that matter in Python:

    • Indentation is the syntax. The lines under each condition are indented by four spaces, and the colon at the end of each condition line is required. elif and else sit at the same indentation as the if they belong to.
    • elif is Python's spelling of "else if". A new if in place of an elif would start a separate chain, and several branches could then run one after another.
    • The variable is created inside the branches. Because the chain ends with an else, exactly one branch always runs, so grade always has a value afterwards. Without the else, a score below 60 would leave grade undefined, and using it later would raise a NameError.

    Edge cases and common mistakes

    • Boundaries. Use >=, not >. With >, a score of exactly 90 would fall to the B branch.
    • Wrong order. If the chain started with score >= 60, every passing score would match it first and become a D, including a 95.
    • Scores outside 0 to 100. The ladder doesn't validate its input: 105 gives "A" and -5 gives "F".

    Testing

    These scores are worth trying, and the ladder gives the following grades:

    ScoreGrade
    90A
    89B
    80B
    79C
    70C
    69D
    60D
    59F
    0F

    Key takeaways

    • Order the branches so each one needs only one limit.
    • Only the first true branch runs.
    • A closing else guarantees the variable gets a value.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free