Chapter 3a - Conditionals
if / elif / elseβ
Branch with if, elif, and else. Conditions are boolean expressions.
var hp = 3
if hp <= 0:
print("down")
elif hp < 5:
print("low")
else:
print("ok")
π§ͺ Try it
Mini quiz β branchesβ
Conditionals
1 / 2What does this print when score is 10?
if score >= 10:print("A")else:print("B")
Pick an answer to see if you are right.
Comparison operatorsβ
| Op | Meaning |
|---|---|
== | equal |
!= | not equal |
< <= > >= | ordering |
and or not | boolean logic |
var alive = hp > 0 and not stunned
π§ͺ Try it
Challenge: grade letterβ
Write letter(score) so it returns:
"A"if score β₯ 90"B"if score β₯ 80"C"otherwise
Hidden tests call your function with different scores.
Indentation
Indent with 4 spaces. Click Reset if an old draft looks flat.
π§ͺ Practice with different scores
Exercisesβ
Build up: parity β HP band β combat gate with boolean logic.
Activity: Even or odd
Write parity(n) so it returns "even" when n % 2 == 0, otherwise "odd".
odd