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
- Start with the top grade.
if score >= 90:setsgrade = "A". - Add
elif score >= 80:for"B". Only scores below 90 reach this test, so it means 80 to 89. - Add
elif score >= 70:for"C", which means 70 to 79. - Add
elif score >= 60:for"D", which means 60 to 69. - 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.
elifandelsesit at the same indentation as theifthey belong to. elifis Python's spelling of "else if". A newifin place of anelifwould 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, sogradealways has a value afterwards. Without theelse, a score below 60 would leavegradeundefined, and using it later would raise aNameError.
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:
| Score | Grade |
|---|---|
| 90 | A |
| 89 | B |
| 80 | B |
| 79 | C |
| 70 | C |
| 69 | D |
| 60 | D |
| 59 | F |
| 0 | F |
Key takeaways
- Order the branches so each one needs only one limit.
- Only the first true branch runs.
- A closing
elseguarantees the variable gets a value.
