Teach Your Kids To Code: A Parent-friendly Guide to Python Programming

(vip2019) #1

78 Chapter 5


In each of these cases, the computer checks a set of conditions:
Is the temperature too cold? Is there something in the path of the
car? Does the email look like spam?
In Chapter 4, we saw a statement that uses a condition to
make a decision: the while statement. In those examples, the condi-
tion told the while loop how many times to run. What if we wanted
to make decisions about whether to run a set of statements at all?
Imagine if we could write one program and let the user decide
whether they wanted circles or other shapes on their spiral. Or
what if we wanted circles and other shapes, like in Figure 5-1?

Figure 5-1: A spiral of rosettes and smaller spirals,
courtesy of an if statement

The statement that makes all this
possible is the if statement. It asks if
something is true, and based on the
answer, it decides whether to perform a
set of actions or skip over them. If the
temperature in a building is fine, the
heating and cooling system doesn’t run,
but if it’s too hot or too cold, the system
turns on. If it’s raining outside, you
Free download pdf