Who is this Project For?
This is a beginner level project for those who are new to Python. At this point, you should already know the basics of variable assignment, if-statements, and while loops. You should also have some previous experience with operators and booleans. These are the concepts that you will use and build from as you code this project.
What Will We Learn?
This project focuses on using booleans and operators, as well as using while loops and
if-statements to control the program’s output. For example, if the player chooses rock and the computer chooses scissors, the player will win. These concepts allow us to write more efficient code while being simple enough for beginners, so let’s get started!
Features to Consider:
The project will start by asking the player to choose between rock, paper, or scissors. Using the random module, the computer will choose either rock, paper, or scissors.
Using if-statements the program will determine whether the player or computer has won the round.
- Paper beats Rock
- Rock beats Scissors
- Scissors beat Paper
In the end, the player state will be set to false again. This will cause the program to start again to allow for continuous gameplay.
This project can be broken down into 5 main steps:
1. Import the random module
2. Create the choice list
3. Define the player and computer variables
4. Create the game’s while loop and if statements
5. Set player to false and redefine computer variable
Step 1: Import Random Module
To start, we’ll need to import randint from the random module. This is a function that will allow us to make the computer variable randomly select either rock, paper, or scissors from the choice list. To do this, let’s type:
Step 2: Create the choice list
Next, let’s make the choice list. This is a list that will contain all the options that players can choose from (Rock, Paper, and Scissors). We’ll do this by typing:
As you can see, I’ve named our list ‘choice’ but you’re free to choose whatever name you want!
Step 3: Define the player and computer variables
After we’ve made the choice list and imported the randint function, we can begin defining the player variables. To do this, we’ll be creating two variables:
- Player: this variable is associated with the player’s choices
- Computer: this variable is associated with a random choice from our choices list
We will need to set the player variable to false for now, as it will have no value until the player has made a choice. This means the computer will choose an option and will wait for the player to make a choice before continuing.
Hint: Remember that when counting, computers start at 0, not 1! This is why our randint command chooses from a range of 0-2 rather than 1-3.
In the end, your code should look like this:
Hint: If you want a way to check how your computer variable changes each time, try adding a line of code saying:
This will print the computer variable, which comes in useful when debugging!
Step 4: Create the game’s while loop and if statements
Now that we’ve defined our computer variable and set our player variable to false, it’s time to create our main game loop! To do this we’ll start by creating a simple while loop and asking the player for their input:
Note: now that the player variable has a value, it is no longer false. The program will then move on to the upcoming if statements.
We’ll create 3 main categories of if statements:
- If player == computer: this will check if the player and computer are the same, and if so, there will be a tie
- If player== [choice] and computer == [choice]: this statement will check which values each variable has and determine who wins. Remember that rock beats scissors, paper beats rock, and scissors beat paper.
- Else: if the player variable is not equal to one of the previously mentioned choices, something is wrong so an error message will show.
Let’s get started with the first if statements:
As you can see we’ve created the line to check whether the player and the computer are the same. We've also added two other statements to handle the possible outcomes when the player chooses rock. Next, we’ll make the same two statements for when the player chooses scissors or paper, keeping in mind the rules of the game. In the end, it should look like this:
Now that we’ve handled all the outcomes, we’ll create our final else statement. This will let the player know if they’ve chosen an invalid answer:
And we’re done with the if statements, we have only one step left!
Step 5: Set player to false and re-define computer variable
Finally, we’ll set the player variable to false again and re-define the computer variable. To do this we’ll type the same command we did at the beginning, and it should look like this:
Hint: remember that the main loop is a while loop. Setting the player to false will reset the loop and let the player play over and over again.
And now we’re done! Feel free to test your code and see how it works. If you’re stuck or have any issues with your code, try reviewing it again. Or check out the code mentioned above as a reference.