๐Ÿ‘Š Rock, ๐ŸคšPaper, or โœŒ๏ธ Scissors? | myBetabox

๐Ÿ‘Š Rock, ๐ŸคšPaper, or โœŒ๏ธ Scissors?

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!

Keyboard Guide

Rock, Paper, or Scissors?

Stuck? Copy & Paste the Code into Repl.it or VS Code

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)

How it looks on VS Code:

The Code Explained

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:

import random

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)