There
is one more key element to writing basic programs: making decisions. Most
programs do not consist of a single recipe. They are capable of doing different
things. For instance, you may write a program for a pet adoption agency that
lists available pets, and then ask the user to choose a pet type. If the user
chooses cats, you want to show cats, and if the user chooses dogs, you want to
show dogs.
In
Python (and in most other programming languages), the most basic way of making
decisions is the "If Statement".
DEFINITION: The if Statement is a
command that checks a condition, and
either does one thing, or another, depending on the answer. Think of the if statement like a
branch in a river. Your program reaches the river branch, and either goes left
or right.
The
syntax for if
statements is complicated. For now, type
in the following program exactly, and run it. Pay special attention to the
indentation. All of the print statements underneath the if statements are
indented by four spaces.
Create a new Python file, type the following and run it:
1.
print "Welcome to the Python Pet Adoption Service."
2.
3.
pet_choice = raw_input("What type of pet would you like to hear about? ")
4.
5.
if pet_choice == "dog":
6.
print "Here are the available dogs:"
7.
print "Rocky is a 3 year old Golden Retriever."
8.
print "Sasha is a 2 year old Siberian Husky."
9.
print "Dozer is a 6 year old Boxer."
10.
11. if pet_choice == "cat":
12. print "Here are the available cats:"
13. print "Sam is a 2 year old Siamese."
14. print "Isis is a 2 year old Tabby."
15.
16. if pet_choice == "python":
17. print "I'm sorry, we don't have any snakes up for adoption."
Run
this program three times, choosing a different animal each time.
Dogs:
Welcome
to the Python Pet Adoption Service.
What
type of pet would you like to hear about? dog
Here
are the available dogs:
Rocky
is a 3 year old Golden Retriever.
Sasha
is a 2 year old Siberian Husky.
Dozer
is a 6 year old Boxer.
Cats:
Welcome
to the Python Pet Adoption Service.
What
type of pet would you like to hear about? cat
Here
are the available cats:
Sam
is a 2 year old Siamese.
Isis
is a 2 year old Tabby.
Pythons:
Welcome
to the Python Pet Adoption Service.
What
type of pet would you like to hear about? python
I'm
sorry, we don't have any snakes up for adoption.
Each
time, you should get a different result. This program makes decisions based on
your input!
if
statements have two pieces of syntax. First are the if statements
themselves.
SYNTAX: An if statement is the
word if,
followed by a condition, followed by
a colon. A condition is something that can be True or False.
For
now, conditions are comparisons. We can check if strings are equal or unequal.
We can check if numbers are equal or unequal (or greater, less than, greater
than or equal, or less than or equal).
These are some acceptable if statements:
The
first if statement
uses the symbol "==". Why double equals? Python already uses the
single equals to do assignment.
Assignment changes values. The double equals tests a condition. "X ==
Y" is the way of asking Python "are X and Y the same?"
TIP: memorize the
difference between single equals and double equals.
Bird_count
= 4 means "Python, make a variable named
Bird_count, and assign it the value 4"
X ==
Y means "Python, are X and Y are the
same?"
Putting
it all together,
means:
If pet_choice is equal to "cat".
The
second part of an if
statement is a block.
DEFINITION: a block is multiple lines of code that are
organized as a unit. If lines of code are sentences, blocks are paragraphs.
In
the pet adoption program, there are four print statements about
dogs, three print
statements about cats, and one print statement about
pythons. When the program is run, it does not print all eight statements. If
the user chooses dogs, Python only prints the dog statements. The way Python
knows to do this is by using blocks.
SYNTAX: An if statement must be
ended with a colon. After the colon, the next line or lines must be indented.
All of the indented lines are treated as a block,
until the next non-indented line.
The block underneath an if statement only gets run if that if statement is True.
In
this diagram, line 5 contains the if statement and the
colon, which starts the block. Lines 6 through 9 are all indented, so they are
treated as a block.
TIP: It is acceptable
to use any amount of indentation. The most common choices are 4 spaces, 2
spaces, or one tab . However, it is critical
to be consistent.
Be
consistent!
Python
uses indentation to read blocks. Code with inconsistent indentations will cause
Python to give an error.
Python
reads this as follows: if pet_choice is equal to "dog", do the
commands in this indentation block. otherwise, skip them.
So,
if pet_choice is dog, all the dog statements are printed. If it is not dog, all the dog statements are
skipped.
We will do another example, for reinforcement. Create a new Python file, type the following and run it:
1.
print "Please choose a cat:"
2.
print "1. Samantha"
3.
print "2. Dolly"
4.
5.
user_choice_string = raw_input("? ")
6.
user_choice = int(user_choice_string)
7.
8.
if user_choice == 1:
9.
print "Congratulations! Samantha is a nice cat."
10.
11. if user_choice == 2:
12. print "Ouch!"
13. print "Bad choice. Ouch!"
14. print "Dolly is a mean cat."
15. print "Ow! Ow, hey! Ow!
Stop that!"
You
should see this output if you choose 1:
Please
choose a cat:
1.
Samantha
2.
Dolly
?
1
Congratulations!
Samantha is a nice cat.
You
should see this output if you choose 2:
Please
choose a cat:
1.
Samantha
2.
Dolly
?
2
Ouch!
Bad
choice. Ouch!
Dolly
is a mean cat.
Ow!
Ow, hey! Ow! Stop that!