Hardware Reference
In-Depth Information
Figure B-8:
Autocomplete of
the print
function in
IDLE.
Conditional Behaviour
Computer programs would be very dull if they always executed the same statements. Luckily,
programs can do different things depending on the data.
In this example, you'll make your insult generator change what it prints depending on the
age of the user. To achieve this conditional behaviour, you'll tell the program to do one thing
if something is true, or if not true, do something else. As a quick test of conditional behav-
iour enter the following code in an empty file:
age = 12
if (age < 16):
print (“young”)
else
print (“old”)
Run the program and you should find it prints young . Change the age variable to be larger
than 15 and run the program again. This time it should print old .
Search WWH ::




Custom Search