Rock-Paper-Scissors Again in Python

It is time to look at the rock-paper-scissors problem and see if it can be coded. It takes more steps, but it is no more complicated than the guess-a-number pro­gram. The code is the same.

  1. Select a choice from the three items rock, paper, or scissors. Save this choice in a variable named choice.

A representation for the three items was when the solution was first de­scribed, where each choice was an integer. However, input reads strings so it should be possible to avoid the conversion to numbers and use the strings directly.

choice = “paper” # Computer chooses paper.

Ask the player for their choice.

Print as prompt message.

print (“Rock-paper-scissors: type in your choice:    “)

  1. Read the player’s selection into a variable named player.

Use input as we did before, but this time read a string and keep it that way. The player must type one of “rock,” “paper,” or “scissors,” or else an error is reported.

player = input ()

  1. If player is equal to choice:
  2. Print the message “Tie. We’ll try again.”

Strings can be compared against each other for equality, so this step is quite simple:

if player == choice:

print (“Game is a tie. Please try again.”)

  1. If player is equal to rock
  2. If choice is equal to scissors, go to Step 17.
  3. There is no “go to Step 17,” but that step simply says that the player wins. Just print that message here.

if player == “rock”:

if choice == “scissors”:

print (“Congratulations. You win.”) else:

print (“Sorry – computer wins.”)

  1. If player is equal to paper
  2. If choice is equal to scissors, go to Step 17.

if player == “paper”:

if choice == “scissors”:

print (“Sorry – computer wins.”) else:

print (“Congratulations. You win.”)

  1. If player is equal to scissors
  2. If choice is equal to rock, go to Step 17.

if player == “scissors”:

if choice == “rock”:

print (“Sorry – computer wins.”)

else:

print (“Congratulations.   You win.”)

This code illustrates a new concept, if not a new language feature. It has if statements that are nested one within the other. Again, it’s not necessary to do this because non-nested statements can implement the same decision. For example,

Nested if statements seem more express ive and communicate the flow of the program better to a human programmer than does the non-nested code.

There is another Python language element that can be used here. Looking at the code, there is no indication when the user makes an error. For example, if the user enters “ROCK” (i.e., all in uppercase letters), then it will not match any of the choices and the program will not indicate this. In fact, it won’t print anything at all. What is really wanted is a sequence of if-else-if-else statements such as

if player == “scissors”: if choice == “rock”: else:

if player == “rock”:

if choice == paper: else:

if player == “scissors”:

## and so on …

Python has a special feature that implements this nesting of if and else: the elif. The elif construct combines an else and an if, and this reduces the amount of indenting that has to be done. The following code snippets do the same thing:

If too many nested if-else statements exist, then the indenting becomes too challenging, whereas the elif allows the same indent level and has the same meaning. In some programs, this is essential, and in general, it is easy to read. Using the elif statement the program for the rock-paper-scissors problem looks like this:

choice = “paper” # Computer chooses paper.

print (“Rock-paper-scissors: type in your choice: player = input () if player == choice:

print (“Game is a tie. Please try again.”)

if player == “rock”:

if choice == “scissors”:

print (“Congratulations. You win.”)

else:

print (“Sorry – computer wins.”)

elif player == “paper”:

if choice == “scissors”:

print (“Sorry – computer wins.”)

else:

print (“Congratulations. You win.”)

elif player == “scissors”:

if choice == “rock”:

print (“Sorry – computer wins.”)

else:

print (“Congratulations. You win.”)

else:

print (“Error: Select one of: rock, paper, scissors”)

Now all of the possible outcomes are handled by the code.

 

Source: Parker James R. (2021), Python: An Introduction to Programming, Mercury Learning and Information; Second edition.

Leave a Reply

Your email address will not be published. Required fields are marked *