Programs need to make decisions. Conditionals let your code take different paths depending on whether something is true or false.
What you'll learn
if,elif, andelseblocks- Comparison operators:
==,!=,<,>,<=,>= - Combining conditions with
and,or,not
Instructions
- The starter code reads a numeric score from stdin.
- Write a grading function that prints the letter grade:
- 90 and above →
"A" - 80–89 →
"B" - 70–79 →
"C" - 60–69 →
"D" - Below 60 →
"F"
- Also print
"Passing"if the grade is D or above, otherwise"Failing".
Example output for input 85:
Score: 85
Grade: B
Passing
Key concepts
if/elif/else checks conditions top-to-bottom and runs the first one that's true:
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "F"
Comparison operators produce True or False:
==equal,!=not equal<less than,>greater than<=less or equal,>=greater or equal
Logical operators combine conditions:
and— both must be trueor— at least one must be truenot— flips true to false
Hints
- Use
eliffor the middle ranges — don't use a separateiffor each. - Indentation matters in Python. Each block under
if/elif/elsemust be indented. - The order of your
elifchecks matters — start from the highest threshold.