Creating ‘Rock, Paper, Scissors’ using Python

In order to create ‘Rock, Paper, Scissors’ we’re going to need to make use of the loop functions that are available in Python.

Firstly, we are going to need to call upon the ‘random’ module which will allow us access to the ‘randint’ method. I will explain why this is important, a bit later.

from random import randint

Once we have defined which module is going to be needed, it is time to create the variables needed to play the game. In this scenario, I have labelled the variable ‘g’ for game, but you can define it any way that works for you. We can add multiple values in this by use of square brackets and separating each variable with a comma.

List variable

Now that we have our variables, it is time to create the players. We are going to start with creating the computer, as that is who we will be playing against. When creating the Computer, we will need to combine ‘randint’ so that a random play can be chosen from the list. As was done with the list, randint is encased in square brackets, with ‘0,2’ in curly brackets. In Python all operators start at zero.

In this instance:

Zero = Rock
One = Paper
Two = Scissors

Computer

When the Computer has been set, its time to add the Player. In this case, I have set the variable as my name. It really makes no difference what you label the variable, so long as you are consistent throughout otherwise, the program will run into errors. The player value needs to be set to ‘False’. The reason for this is when the game starts, the operator will automatically change to ‘True’ allowing the While loop to run.

Player

Now that all the variables have been set, it is time to move onto the main body of the program. Inside the ‘While Loop’ are nested ‘if’ and ‘elif’ statements which control the various outcomes of the game. It is important to define the tie breaker early on and then defining the reasons that the game resulted in either a Win, Loss or Draw.

As you will be able to see from the following screenshot, I have added ‘\n’ at the end of the user input. This will end the string on a new line and makes the overall responses within the game easier to read.

Finally, at the end of the ‘While Loop’, it is important to add an else statement for an invalid play. This will allow the game to continue if an incorrect value is entered by the user.

While Loop

Two more lines of code are required at the end of the While Loop. These two lines are required, so that the game will run from one instance to the next in an infinite loop.

The player variable needs to be set back to ‘False’ at the end of the play for this to occur, and the computer is listed here for the same reason, so that it will continue to be assigned random plays.

Infinite Loop

For any fans of the Big Bang Theory, I am hoping to try and expand on this code to create a ‘Rock, Paper, Scissors, Lizard, Spock’ game, but first I had to work through this code to understand how it should work. In addition to this, I want to try and build on it further by adding a graphical interface.

I also want to say that I got the code for this game from this website, they have a lot of great tutorials and content for anybody learning Python.

Published by Daniel Wray

IT Support Engineer; Studying for CompTIA Network+. 100 Days of Python Challenge. Aspiring blogger. Passion for Learning and Teaching! https://ko-fi.com/danielwray4

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: