Hardware Reference
In-Depth Information
5.
Next, create conditions depending on the player's answer. You will need one
condition if the player chooses
left
and another if she chooses
right
. You
may remember using conditionals in Scratch in Adventure 3. In Python, you use
if,
elif
(else if) and
else
to check conditions:
if direction1 == “left”:
print(“You walk to the cave and notice there is an ;
opening.”)
elif direction1 == “right”:
print(“You walk to the beach but remember you do ;
not have any swimwear.”)
else:
print(“You think for a while”)
if
,
elif
and
else
are the Python words used to check conditions. In the
preceding code, if the player types
left
, the program prints the statement, “You
walk to the cave and notice there is an opening”; else if (
elif
) the player types
in
right
, the program prints a diferent piece of text. Finally, if the player types
in any answer that is not
left
or
right
(
else
), the program prints, “You
think for a while”. Figure 5-8 shows this code in the text editor.
FIGURE 5-8
Using conditionals in a Python adventure game