Kids Coding
Yes or No (If)
if and else pick a path. Compare two values. Print a different message.
Pick a path
if checks a yes-or-no question. If the answer is yes, Python runs the indented lines. else is the other path.
Password door
word = "open"
if word == "open":
print("Come in")
else:
print("Stay out")Compare
| Check | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< | less than |
> | greater than |
= puts a value in a box. == asks “are these the same?” Mixing them up is a common first bug.
Indentation is the block
The lines under if must be indented (usually four spaces). That indent is how Python knows which lines belong to the if.
A number check
score = 12
if score >= 10:
print("You passed")
else:
print("Try again")Real life
Looking out the window before you leave.
If it is raining, take a coat. Else, leave it. Two paths. One check.
Coat or not
weather = "rain"
if weather == "rain":
print("Take the coat")
else:
print("Just a jumper")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: If age is 10 or more, print You can ride. Otherwise print Not yet. Try both values.
Show solution
age = 10
if age >= 10:
print("You can ride")
else:
print("Not yet")