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

(vip2019) #1

84 Chapter 5


are built-in constant values in Python. Python will not understand
if you type True as true without the capital T, and the same goes for
False.

You’re Not Old Enough!


Let’s write a program that uses Boolean conditional expressions to
see if you’re old enough to drive a car. Type the following in a new
window and save it as OldEnough.py.
OldEnough.py

u driving_age = eval(input("What is the legal driving age where you live? "))
v your_age = eval(input("How old are you? "))
w if your_age >= driving_age:
x print("You're old enough to drive!")
y if your_age < driving_age:
z print("Sorry, you can drive in", driving_age - your_age, "ye ars.")


At u, we ask the user for the legal driving age in their area,
evaluate the number they enter, and store that value in the vari-
able driving_age. At v, we ask for the user’s current age and store
that number in your_age.

The if statement at w checks to see if the user’s current age
is greater than or equal to the driving age. If w evaluates to True,
the program runs the code at x and prints, "You're old enough
to drive!". If the condition at w evaluates to False, the program
skips x and goes to y. At y, we check if the user’s age is less than
the driving age. If so, the program runs the code at z and tells the
user how many years it’ll be until they can drive by subtracting
driving_age from your_age and printing the result. Figure 5-4 shows
the results of this program for my son and me.
Free download pdf