Branching and
Random
Decision-making is an integral part of
programming. In other words, programs
start to get interesting when they behave
differently according to what the values of
variables are, or how they compare to one
another. Our guessing game program
provides a perfect example.
import random number = random.randint(10,99)
guess = input("Enter a two-digit number: ")
if guess < number:
print "Nice try, but too low."
elif guess > number:
print "Good effort, but too high."
else:
print "I can't believe you've guessed it!"
The first two lines of code generate the random
number. (Note that you only need to use
the import command once at the start of the
program.) Also be aware that typing the
range (10,99) will cause
the computer to choose a number from 10 to 99 inclusive.
So to obtain a random number in the 40s, for
instance, type random.randint(40,49).
The program now chooses one of several options,
depending upon how these values compare.
If guess
is less than number
the program prints the first statement, then
skips over the following four lines of code.
Otherwise it next checks whether guess is
more than number,
and if so prints the middle statement, then
skips over the next two lines of code.
Only if neither of the first two conditions hold
true (in other words, when guess is
equal to number)
does the program print the final statement in
the list.
Notice that each of the branching statements
ends with a colon, and the subsequent line is
indented. This is how we indicate to
Python that the outcome of the if (or
elif
or else)
statement is coming up next, and that it should
execute exactly the indented lines of code in
case the condition is satisfied. It is
fine to include several indented lines of code,
even to have several levels of indentation.
It is allowable to include as many elif
commands as desired, or none at all. (Note
that elif
is short for "else if".) One can also
leave off the final else statement if
desired. Here are several variations on
the theme to illustrate these alternatives.
if guess < 10:
print "That's not a two-digit number, bozo."
elif guess > 99:
print "You're not following the instructions." elif guess > 70:
print "Why not make the first guess around 50???"
Let's think carefully about how these lines of
code will execute in various situations.
If guess
equals 88 then neither of the first two
conditions are satisfied, so the program makes
it all the way to the third print
statement. On the other hand, if guess
is 108 then the second line will execute but
not the third. That's how elif
commands behave. Meanwhile if guess
equals 6 only the first statement will print,
and if guess
is 44 then none of the statements will
print.
The next variations introduces two new tests—we
use ==
to check for equality and !=
to check for nonequality.
if guess == 42:
print "That's the answer to everything!"
else:
print "You're getting warm."
if guess != 99:
print "Try a higher number."
The top program will output You're getting warm.
most of the time, but gives a different response
if the value of guess
is 42. The bottom program outputs Try a higher number.
almost always. Observe that when the value
of guess
is actually 99, then the program does
nothing.
HEADS UP!
One of the most common mistakes to make as a
programmer is to use a single equal sign when
testing whether two quantities are equal.
This is a subtle point. We use =
when assigning a value to a variable, as in age = 8,
but we type ==
when testing for equality, as in if age == 9:
It's important to understand the difference!
Finally, it's worth pointing out that one can
also use <=
and >=
to compare two values. However, observe
that for integers, age
<= 19 is the same as
age < 20.
In other words, for most practical purposes the
commands <
and >
are sufficient.
|
|