Rock Paper Scissoes Using Python
Coding is a really helpful skill to learn and can make many tasks a lot easier. Today we will be leaning how to code a simple rock paper scissors game using the coding language Python. If this is your first time coding I know it can seem daunting, however this set of instructions is designed for those with little to no programming experience so if you follow them correctly you should end up with a fully functioning program.
For this task all you will need is python and an ide (integrated development environment) which I will show how to download and install. So if you think you are ready to start your coding journey, head on to step 1.
Installing Python
Before we can start programming, we must first install python to our device. To do so, click here. This will take you to python.org download page as shown above. One you are there, click download python 3.9.2. You should than see a download window pop up on the bottom left of your browser. Once that is ready click on it. That will open up the installer. Once the installer is open simply click install now.
Downloading PyCharm
To actually start coding, we must first install an IDE which is a type of software that actually lets us write our code. We will be using PyCharm as it is very beginner friendly but if you have any other IDE installed you may use that instead. to start, start click here. This will take you to the download page shown above. First choose your OS, windows, mac, or linux and click the black download button under community. That will start the download which you should is at the bottom left of your window.
Setting Up Pycharm
Once it is done downloading, click on the installer. You should see a window that say 'Welcome to PyCharm Community Edition Setup". Click next until you get to the final window. Check the the box that says "Run PyCharm community edition" and click finish. That should open up PyCharm. Were almsot ready to start coding!
How to Start Using PyCharm
This video will show you how to open create a project and a python file.
Starting Our Code
Now that we have python and PyCharm installed and a python project open, We can start writing our code (I will be including a picture of the code we are writing at the start if each step so that you have a visual sense of what you should be typing and how it should look).
First thing we want to do is type the statement import random on line 1 in all lowercase. Python is case-sensitive so make sure that you do not user upper or lower case arbitrarily.
Import is a keyword use to bring in other modules, in this case we are importing the random module which we will use later on
On lines 3 we will type while True. Notice that True has an upper case t and ends with a colon.
The while keyword is known as a loop. This is a control structure that is in many programing languages and allows us execute code repeatedly depending on the Boolean condition. A Boolean condition in basically like an equation but instead of a numeric output the value is either true or false. For our loop we want it to run it until the user does not want to play, so we put True. Another thing to note, to make the loop valid we must end with a colon otherwise we will get an error during execution.
User and Computer Input(lines 4-5)
Next, we want to ask the user for their choice, either rock, paper, or scissors and we want the computer to also pick the same choice, so we type the following:
playerChoice = input('Please enter r, p, or s ').lower()
compChoice = random.choice(['r', 'p', 's'])
Notice how lines 4 and 5 are indented. This is important because if we want the code to be part of the loop, we must indent the lines otherwise they are not included in the loop
On line 4 we first declare a variable we call playerChoice and we set that equal to input(). The input keyword is used to get keyboard input from the user. Inside the parentheses we ask them to enter r for rock, p for paper, and s for scissors. The .lower() is known as sting method. It will make sure that if the player enters a capital R, P , or S, it is turned in to a lower case so than when we compare to the computer choice, we have no issues,
On line 5 we declare the variable compChoice and set it equal to random.choice(). If we recall, random was the module we imported on line 1. We will use it to get a choice from the computer. The .choice() that follows random is a method. Inside the parenthesis of .choice() we want to put the choices that computer will chose, either r, p, or s. we do that by first adding brackets inside the parenthesis and inside the brackets we add ‘r’, ‘p’, ‘s’. we have to make sure that we put them in single quotations and split them up with commas so that they are considered strings.
The Outcomes
After that we want to compare the players answer from the computers. If your familiar with rock paper scissors then you should know that depending on what you picked, you either win, lose, or tie. Here is a table of the outcomes we will be coding
Rock Outcome(lines 7-13)
First, we will code the outcomes if the player chooses rock. To do that We will type the following using if statements (lines-7-13):
if playerChoice == 'r':
if compChoice == 'r':
print('You both chose rock. Its a tie')
elif compChoice == 'p':
print('You chose rock the computer chose paper. You lose')
elif compChoice == 's':
print('You chose rock the computer chose scissors. You win')
(refer to the picture above for proper formatting)
An If statement is a conditional statement which only executes if the specified Boolean condition is true or false. The statement on line 7 first checks if the player input r for rock. If they did, then that condition is true so the program will go to line 8-13 which is another if/elif statement. Elif statements are similar to if statements but can only be used after an if. They continue an If statement when there are more than 2 outcomes. In this case it will compare the players choice to the computers and output the outcomes, win lose or tie. And using the print() function display the result to the console. The code for paper and scissors will be pretty similar.
one thing to note: = vs ==, in the if statement we use == instead of =. The reason for that is because 2 equals are used to see if 2 things are equal to each other while a single equal is used to set something equal to something else. A good way to think about this is: x = y means x is equal to y and x == y means if x is equal to y.
Paper and Scissors Code(lines 14 -27)
On line 14 we do the same thing we did on line 7 but for paper and the same on line 21 for scissors.
Ending the If Statement(line 28-29)
on lines 28-29 we add and else statement to end our first if statement. This ensures that if the player inputs something other than r, p, or s, we tell them them that is an invalid statement. While this is not required it is a good thing to add so that the code does not just end abruptly and confuse the player.
Replay Feature(31-39)/end
Now that we are done with our comparisons of the user and computer options to see who wins, we will want to give the user an option to play again or quit. to do that you type the following
print(' ')
replay = input('Press y to play again or anything else to quit')
if replay == 'y':
pass
else:
print('Thanks for playing') break
on line 31 we add an empty print line to give us some space. Line 33 declares a variable know as replay which will ask the user if they would like to play again or quit. On lines 35-39 we us an if statement and return the user to the beginning of the loop using the pass keyword or exit end the program using the break statement.
And that is the end of our code. To run the code, right click on the screen and click the option with the big green triangle which says run ‘name of your file’.
(If you are having trouble with your code, the picture above is the what your code should look like when you finished. It is good reference to look at if you are are having any errors when you run your own code)
The End!
And thats it, If this was your first time coding, I hope you enjoyed it and continue to learn more about it. Coding can seem difficult but with enough effort anyone learn how to code.