Learn how to make a Rock Paper Scissors game using lists and dictionaries from Raph, a member of our software team!

You can access the source code of the game here.

1. First, we’re going to define what lists and dictionaries are.

2. List: a number of items contained in a structure.

Here’s how lists are formatted:

list = [‘itemOne’, ‘itemTwo’, ‘itemThree’]

And here’s an example:

fruits = [‘strawberry’, ‘apple’, ‘mango’]

In this example, we have a list of strings named fruits, the first item is strawberry, the second is apple, and the third is mango. String items are denoted by quotes and separated by commas. In int value lists, quotes are not needed.

The values have an index, starting at 0. Typing fruits[0] = strawberry, fruits[1] = apple, and fruits[2] = mango.

3. Dictionary: a set of items relating to one another that are indexed.

Here’s how dictionaries are formatted:

dict = { “key” : “name” }

And here’s an example:

foods = { “fruit” : “banana” “soda” : “sprite” “chips” : “pringles”}

A dictionary is similar to a list, but instead of index numbers, each item in the list has its own key, like a password.

In this example, we have a dictionary of different foods. In this dictionary, “fruit” is a key to the item “banana”, “soda” is a key to “sprite”, and “chips” is the key to “pringles”.

foods[“fruit”] will output “banana”.

4. Now that we know what lists and dictionaries are, we’re going to apply both of them and program a Rock, Paper, Scissors game.

We will be coding in Python for this tutorial. First, we’ll define what we want in this game.

  1. User Input
  2. Random Computer Choice
  3. The Option to Play Again

Not only will we be applying lists and dictionaries, we’ll also be incorporating some basic user input and randomization.

5. First, we’ll insert “import random”.

This lets us use the random module from the Python library, so that we can make the computer’s choice completely random.

import random

6. We’ll now initialize our first dictionary.

This dictionary, named “answers” will be used to see if the user wants to play another game after playing one.

We’ll have two keys, one for yes, play again, and one for no, quit the game.

Our first key, yes, will have a list of different ways to type yes tied to it. Multiple ways to type yes or true are “yes, y, 1”. So when we ask the player if they want to play again, they can type any of these and we know they want to play again.

The second key will be no. The multiple ways to say no or false will be “no, n, 0”. So likewise, if the user types any of these, we know they want to end the game.

answers = {"yes" : ["yes", "y", "1"], "no" : ["no", "n", "0"]}

7. We’ll now create another list called “options”, and it will hold the different moves that can be made in this game, “rock, paper, scissors.”

options = [“rock”, “paper”, “scissors”]

It’s important to note the indexes of each item in this list. For this list, options[0] = rock, options[1] = paper, options[2] = scissors.

8. Now, we’ll be making a more complex dictionary.

This one, named “results”, will hold all the different ways a game of rock, paper, scissors can end.

Two things to note:

  1. This is where we need to know the indexes of our options list, as it will affect what moves we’re comparing.
  2. We’ll be programming from the user’s perspective, doing the user’s choice vs. the computer choice. So, when referring to a pair of moves, the user’s choice will always be stated first.

We’ll start by defining the “win” key, all the possible ways the player can win using the options list.

  • For example, one way to win is if the player chose rock and the computer chose scissors. This would be denoted with the pair, (options[0], options[2]), with the options list defined the way we did.
  • So all the ways the player to win are: rock vs scissors, paper vs rock, and scissors vs paper.
  • The key for winning would look like this:
“win” : [(options[0], options[2]),(options[1], options[0]),(options[2], options[1])]

We’ll now do the same for the other two results: lose and tie.

  • The final dictionary should look something like this:
results = {"win"  : [(options[0], options[2]),(options[1], options[0]),(options[2], options[1])],"lose" : [(options[0], options[1]),(options[1], options[2]),(options[2], options[0])],"tie"  : [(options[0], options[0]),(options[1], options[1]),(options[2], options[2])]}

9. Now we’ll begin the actual game.

We’re going to want to put the game in a while loop. We’ll initialize a boolean variable outside of the loop to be true, so that the game can be played at least once. At the end of the loop we’ll ask if they want to keep playing, and their result will determine the value of that boolean variable.

keepPlaying = True
validSelection = False
while keepPlaying:
checkReplay = ""
while checkReplay.lower() not in (answers["yes"] + answers["no"]):
checkReplay = input("Wanna play again?: ")
validSelection = False
if checkReplay.lower() in answers["no"]:
print("\nThanks for Playing!")
keepPlaying = False

This is the general loop of the game. The code from the line checkReplay down will be at the very end of the loop.

  • checkReplay is a string variable that will hold the user’s response when asked if they want to play again.
  • The while loop will keep asking if the player wants to play again if they don’t type one of the names in the results dictionary.
  • The if statement will break the loop if the player types one of the answers tied to the “no” key. Otherwise, the program will loop and a new game will begin.

Inside the while loop, we’ll begin with getting the user’s choice. We want to make sure they choose either rock, paper, or scissors, so we’ll keep looping until they make a valid choice.

while not validSelection:
userSelection = input("rock, paper or scissors?: ").lower().strip()
if(userSelection in options):
validSelection = True
else:
print("Please try again. Enter a valid option.\n")

Now we’ll randomly determine the computer’s decision. Importing the random module earlier allows us to make a random choice from the options list.

computerSelection = random.choice(options)

Lastly, we’ll be comparing the player and computer’s moves, and returning the result of the game. We will be telling the player how they did against the computer.

  • for outcome in results:
for outcome in results: 
if (userSelection, computerSelection) in results[outcome]:
print(“You {} against your opponent!\n”.format(outcome))
  • We’ll start by making a for loop, to look through all the possible combinations in the results dictionary.
  • If the pair of moves is equal to a pair in the dictionary, we will output the game result associated with that pair.

Now, to put this pieces all together, the final game loop should look something like this:

Ta da!

10. Congratulations!

You have completed the game and hopefully you learned a bit about lists, dictionaries, and other programming concepts!

This tutorial was written by Raph Napinas. You can connect with him on Instagram at @kuya.raph!

We’re changing the world with code, and also running a blog now, apparently. Catch us at helloo-world.com!