10. Letter grade
Write a function grade(score) that takes a test score from 0 to 100 and returns its letter grade, as a string of
one capital letter:
| Score | Grade |
|---|---|
| 90 to 100 | "A" |
| 80 to 89 | "B" |
| 70 to 79 | "C" |
| 60 to 69 | "D" |
| 0 to 59 | "F" |
Both ends of each range count: exactly 90 is an "A", exactly 80 is a "B", and so on. There is no "E".
Example 1
Input
score = 95Output
"A"Explanation95 is in the range 90 to 100.
Example 2
Input
score = 72Output
"C"Explanation72 is in the range 70 to 79.
Example 3
Input
score = 60Output
"D"Explanation60 is the lowest score that gets a D.
Constraints
scoreis a whole number from 0 to 100.
Submit also runs 7 more tests you can't see.
Practises If / Elif / Else (lesson 8).
- Use one
if, thenelifs, then anelse: Python runs only the first branch whose test isTrue. - Test from the top grade down:
if score >= 90:first, thenelif score >= 80:, and so on. 95 is also>= 80, so the order matters. - Use
>=, not>, so a score of exactly 90, 80, 70 or 60 gets the higher grade. Everything below 60 falls to theelse:"F".
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Was this challenge helpful?
Thanks!
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
Code
def grade(score):
"""Turn a score from 0 to 100 into a letter grade.
score: int, from 0 to 100
Returns: str, "A", "B", "C", "D" or "F"
"""
# Write your code here
pass
Run your code to see how it does on the test cases, or Submit to check every test.