Conditionalsconditionals-grades
LessonΒ·difficulty 1/5Β·~10 min
Conditionals: letter grades
Conditionals
Decisions in code use if, elif, and else:
python
if temperature > 30:
advice = "Drink water."
elif temperature > 15:
advice = "Light jacket."
else:
advice = "Bundle up."
The first branch whose condition is True runs. elif is short for
"else if" and there can be as many as you need. else is optional but
catches everything else.
Your task
Write a function grade(score) that takes an integer 0β100 and returns
the matching letter grade as a string:
| Score range | Returns |
|---|---|
| β₯ 90 | "A" |
| 80 β 89 | "B" |
| 70 β 79 | "C" |
| 60 β 69 | "D" |
| anything else | "F" |
Example:
python
grade(95) # "A"
grade(72) # "C"
grade(40) # "F"
Tests
- 100 -> A
- 90 -> A
- 85 -> B
- 70 -> C
- 65 -> D
- 0 -> F
- boundary 89 -> B (hidden)
Stuck? Ask for a spark
warming up
Loading editorβ¦
$ Console output will appear here.