← All lessonsconditionals-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)
loading…
Loading editor…

Console output will appear here.

AI tutor
Sign in to chat with the tutor.