Skip to main content

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​

Quiz

Conditionals

1 / 2

What 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​

OpMeaning
==equal
!=not equal
< <= > >=ordering
and or notboolean 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