Rock, Paper, Scissors is probably one of the first games you learned how to play with parents or friends! We will be creating our own version of the game where we play against the computer today!
Let’s start by creating a simple version that will allow us to play one round of the game and then we can add on to it and make the program keep score!
import random player_wins = 0 comp_wins = 0 print("Welcome to RPS") player_choice = input("Do you pick [rock], [paper], or [scissors]?: ") comp_choice = random.choice(["rock", "paper", "scissors"]) print("Player chose " + player_choice + " and Computer chose " + comp_choice)
First, we will need the computer to be able to make random choices for us. To do this we need to import something called a package.
🌟 A package is a collection of pre-written code that will make our time programming much easier.
To import this package, simply type the following at the top of your file:
Now that we have imported the package we can start using it to make random choices for the computer!
Next, let’s code a way to record the number of times a player and the computer wins a round. We can do this by coding a variable that will store this info.
🌟 A variable stores information and values such as characters (letters/words/symbols), numbers, and booleans (true/false statements). Variables can change.
Since we haven’t played any matches yet, let’s set the number of wins for both to 0.
player_wins = 0 comp_wins = 0
Whenever we want the game to say something for the player to see, we will tell the program to print. We want to print a greeting in the form of a string inside quotation marks.
🌟 Print sends text, variables, and other objects to the screen for the user to see.
🌟 A string is a sequence of characters (numbers, letters, symbols and spaces) and start and end with quotation marks. In this example, the string is “Welcome to RPS”, including the spaces between words.
print("Welcome to RPS")
Now we need to ask the player what they want to choose and save it in a variable so that we can use it later. Input is the function we’ll use to interpret user choices.
🌟 Functions are packets of code that do a certain task and are always followed by ( ). Functions we use in this section are print(), input(), and random.choice().
To do this we will type the following code on the next line:
player_choice = input("Do you pick [rock], [paper], or [scissors]?: ")
It is important to note that I put a space after the colon. I did this so that when we type our answer it will not start right at the colon.
Once we have saved the player input, we need to randomly decide what the computer is going to pick. To do this, we use a function from the random package called choice. Take a look at the following code to see how it works:
comp_choice = random.choice(["rock", "paper", "scissors"])
This function takes a list, which is a group of items contained in square brackets, and randomly chooses an item from it. These will be the options the computer can choose from. In this case, our list is [“rock”, “paper”, “scissors”].
🌟 Lists can contain several things including numbers, strings, variables, and Booleans. Lists use square brackets [ ] and list items are separated by commas.
Now let’s display which choices the player and the computer made. To see them, we need to print them to the screen. We will concatenate a string using these choices.
🌟 Concatenation is smushing two strings together as they are written.
print("Player chose " + player_choice + " and Computer chose " + comp_choice)