118 Chapter 6
whether the player and the computer picked the same item; if so, we
tell the user that the outcome was a tie. Otherwise, we check at {
whether the user selected rock. Inside the elif statement at {, we
nest an if statement to see whether the computer picked scissors.
If our player picks rock and the computer chooses scissors, rock
crushes scissors, and the player wins! If it’s not rock and rock, and
if the computer didn’t pick scissors, then the computer must have
picked paper, and we print that the computer wins.
At the remaining two elif statements, | and }, we do the
same testing to check for wins when the user picks paper or scis-
sors. If none of those statements was true, we let the user know
they’ve entered something that did not compute: either they made
a choice that doesn’t exist, or they misspelled their choice. Finally,
at ~, we ask the user for their next choice before beginning the
game loop all over again (a new round). See Figure 6-4 for a
sample run of the program.
Figure 6-4: Thanks to random choices by the computer,
RockPaperScissors.py is a fun game!
Sometimes the user wins, sometimes the computer wins, and
sometimes they tie. Because the outcome is somewhat random, the
game is fun enough to play to pass a little time. Now that we have
a sense of how a game with two players can use the computer’s
random choices, let’s try creating a card game.