Rock, Paper, Scissor Game with Python

Paul Zhao
Paul Zhao Projects
Published in
12 min readOct 3, 2020
Python with games

In this project, we’ll be touching upon Python with an interesting game called Rock, Paper, Scissor. We are going to get started from scratch and then dive deeper as we go along. Every single step will be explained before we move to the next one.

Prerequisites:

1. Homebrew installed for Mac installation (Chocolatey if using Windows)

Installing Homebrew

Open terminal and type in

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
.
.
.
==> Installation successful!
==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
https://docs.brew.sh/Analytics
No analytics data has been sent yet (or will be during this `install` run).
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations
==> Next steps:
- Run `brew help` to get started
- Further documentation:
https://docs.brew.sh

Verify Homebrew installation

$ brew --version
Homebrew 2.4.16
Homebrew/homebrew-core (git revision 23bea; last commit 2020-09-04)
Homebrew/homebrew-cask (git revision 5beb1; last commit 2020-09-05)

Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

2. Python (the version must be 3.7 to make sure that all functions will be available to present intended outcome) is required to be installed

Installing Python3

Now, we can install Python 3:

$ brew install python
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
==> Updated Casks
session
Warning: python@3.8 3.8.5 is already installed and up-to-date
### my python was preinstalled, you may see different installation process. And it may take a while before python is fully installed

Verify is your python3 is installed

$ python3 --version
Python 3.8.5

Notes: you may set your default python as latest version by applying following code

$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.8 /usr/local/bin/python

Base

With that said, let us kick off our project.

Create our very first file as rock_paper_scissor.py

print("...rock...")
print("...paper...")
print("...scissors...")
player1 = input("(enter Player 1's choice): ")
player2 = input("(enter Player 2's choice): ")
print("SHOOT!")

Test it

$ python3 rock_paper_scissor.py 
...rock...
...paper...
...scissors...
(enter Player 1's choice): rock
(enter Player 2's choice): rock
SHOOT!

At this point, we only print out rock, paper and scissors. Then we ask to input from player1 and player2. No matter what is being typed in, the outcome will be printed out as SHOOT!

Version 1

Now as we build up our foundation, we move on to build our project for intended outcome.

Create a file named v1.py

print("Rock...")
print("Paper...")
print("Scissors...")
player1 = input("Player 1, make your move: ")
player2 = input("Player 2, make your move: ")
if player1 == "rock" and player2 == "scissors":
print("player1 wins!")
elif player1 == "rock" and player2 == "paper":
print("player2 wins!")
elif player1 == "paper" and player2 == "rock":
print("player1 wins!")
elif player1 == "paper" and player2 == "scissors":
print("player2 wins!")
elif player1 == "scissors" and player2 == "rock":
print("player2 wins!")
elif player1 == "scissors" and player2 == "paper":
print("player1 wins!")
elif player1 == player2:
print("It's a tie!")
else:
print("something went wrong")

Test it

Notes: Be aware that everything you type is case-sensitive as it its intended by basic rule of python 3

Test 1

$ python3 v1.py
Rock...
Paper...
Scissors...
Player 1, make your move: paper
Player 2, make your move: rock
player1 wins!

Test 2

$ python3 v1.py
Rock...
Paper...
Scissors...
Player 1, make your move: scissors
Player 2, make your move: rock
player2 wins!

Test 3

$ python3 v1.py
Rock...
Paper...
Scissors...
Player 1, make your move: rock
Player 2, make your move: rock
It's a tie!

OK. Let us digest how version 1 works. We still would print out rock, paper, scissors. Then we ask for input from both player1 and player2. After that, we have our if statement to serve. It may look a bit of clumsy to use and to present our coding logic. But it does work. We provide 6 different scenarios, in which either player1 or player2 may win, then print out whoever is supposed to win based on rock, paper, scissors’ game rule. On more thing to add, don’t forget the tie scenario. That’s why else comes in to play its role. If player1’s choice = players’s choice, then a tie is reached.

Version 2

As we see from version 1, the logic is right there, but the coding looks a bit of clumsy as we stated, so now let’s make it more human readable and understandable by rendering a little change.

Create a file named v2.py

print("Rock...")
print("Paper...")
print("Scissors...")
player1 = input("Player 1, make your move: ")
print("***NO CHEATING!\n\n" * 20)
player2 = input("Player 2, make your move: ")
if player1 == player2:
print("It's a tie!")
elif player1 == "rock":
if player2 == "scissors":
print("player1 wins!")
elif player2 == "paper":
print("player2 wins!")
elif player1 == "paper":
if player2 == "rock":
print("player1 wins!")
elif player2 == "scissors":
print("player2 wins!")
elif player1 == "scissors":
if player2 == "paper":
print("player1 wins!")
elif player2 == "rock":
print("player2 wins!")
else:
print("something went wrong")

Test it

Test 1

$ python3 v2.py
Rock...
Paper...
Scissors...
Player 1, make your move: rock
***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!


Player 2, make your move: scissors
player1 wins!

Test 2

$ python3 v2.py
Rock...
Paper...
Scissors...
Player 1, make your move: paper
***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!


Player 2, make your move: scissors
player2 wins!

Test 3

$ python3 v2.py
Rock...
Paper...
Scissors...
Player 1, make your move: rock
***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!

***NO CHEATING!


Player 2, make your move: rock
It's a tie!

Let us break it down. First print out Rock, Paper, Scissors. Then we ask to input player1’s choice, followed by 20 lines of no cheating row. After that, we ask to input player2’s choice.

Now let us move to logic section. Given player1’s choice = player2’s choice, tie as an outcome before diving into other alternatives. By doing so, it makes sure tie option would not go through any other alternatives before reaching to a tie. Then in 3 elif scenarios for player1 == rock, paper or scissors. Under each circumstance, player2 prints either paper or scissors; rock or scissors; paper or rock. In else section, print out went wrong if all previous scenarios did not happen.

Simplified Version 1

As we move on to a simplified version 1, rather than individually checking for the conditions that lead to a player2 win, they’re all lumped together using the else :

Create a file named clean.py

p1 = input("Player 1: rock, paper, or scissors ")
p2 = input("Player 2: rock, paper, or scissors ")

if p1 == p2:
print("Draw")
elif p1 == "rock" and p2 == "scissors":
print("p1 wins")
elif p1 == "paper" and p2 == "rock":
print("p1 wins")
elif p1 == "scissors" and p2 == "paper":
print("p1 wins")
else:
print("p2 wins")

Test 1

$ python3 clean.py
Player 1: rock, paper, or scissors rock
Player 2: rock, paper, or scissors rock
Draw

Test 2

$ python3 clean.py
Player 1: rock, paper, or scissors rock
Player 2: rock, paper, or scissors paper
p2 wins

Test 3

$ python3 clean.py
Player 1: rock, paper, or scissors rock
Player 2: rock, paper, or scissors scissors
p1 wins

Let us know digest the pros and cons of this solution. First of all, with tie scenario up front, this solution allows player1 to have 3 scenarios, which player1 wins. And the opposite of these 3 scenarios will trigger player2 wins. By doing so, we condensed our solution with clear mind.

The only issue with this version is that it will print “p2 wins” if either user enters an invalid move like “pizza”. It doesn’t have the error-checking of the previous solution detailed. But as far as the pure RPS logic, this is a clean approach.

Version 3

Did you get bored to play on your own, now let’s play the game with AI.

Create a file named v3.py

import random ### Here you must define random for use. Otherwise, python3 will return as "radiant" not defined
player = input("Player, make your move: ").lower()
rand_num = random.randint(0,2)
if rand_num == 0:
computer = "rock"
elif rand_num == 1:
computer = "paper"
else:
computer = "scissors"

print(f"Computer plays {computer}" )
if player == computer:
print("It's a tie!")
elif player == "rock":
if computer == "scissors":
print("player wins!")
else:
print("computer wins!")
elif player == "paper":
if computer == "rock":
print("player wins!")
else:
print("computer wins!")
elif player == "scissors":
if computer == "paper":
print("player wins!")
else:
print("computer wins!")
else:
print("Please enter a valid move!")

Test 1

$ python3 v3.py
Player, make your move: rock
Computer plays paper
computer wins!

Test 2

$ python3 v3.py
Player, make your move: rock
Computer plays scissors
player wins!

Test 3

$ python3 v3.py
Player, make your move: rock
Computer plays rock
It's a tie!

For the logic section, we firstly define our computer outcomes using if, elif and else statements, then print out rock, paper or scissors.

Then we move on to games in between player and computer. Scenario 1: player = computer, then when player plays rock, paper, scissors respectively, computer may play with (scissors ,else), (rock, else), (paper, else) as alternatives.

If, none of above applies, then print out “Please enter a valid move!”

Bonus Section

Now let us play a guessing game!

We will go from scratch.

1 Create a file named starter.py (basic version)

import randomrandom_number = random.randint(1,10)

In this step, we import random and give it random number between 1 and 10

Now, we will build up our guessing game structure

2 Create a file named guessing.py (version 1)

Test 1

$ python3 guessing.py
Pick a number from 1 to 10: 3
YOU WON!!!!
3

Test 2

$ python3 guessing.py
Pick a number from 1 to 10: 7
TOO HIGH!
Pick a number from 1 to 10: 5
TOO HIGH!
Pick a number from 1 to 10: 4
TOO HIGH!
Pick a number from 1 to 10: 3
TOO HIGH!
Pick a number from 1 to 10: 2
YOU WON!!!!
2

Test 3

$ python3 guessing.py
Pick a number from 1 to 10: 8
TOO HIGH!
Pick a number from 1 to 10: 5
TOO LOW!
Pick a number from 1 to 10: 6
YOU WON!!!!
6

Firstly, we need to define guess. Then in the while loop, we setup guess as a value that is not equal to the random_number we provided. While, if it’s exactly the same number as random_number, then print out “YOU WIN!” straight away. If not, the while loop will go through the following process: if the number guessed is slower than number generated by computer, then it prints out “TOO LOW!”, if the number guessed is higher than number generated by computer, then it prints out “TOO HIGH!”, we will keep guessing until the number guessed = number generated by computer, then it prints out “YOU WON!”

3 Create a file named guessingv2.py (version 2)

import randomrandom_number = random.randint(1,10)  # numbers 1 - 10
guess = None
while True:
guess = input("Pick a number from 1 to 10: ")
guess = int(guess)
if guess < random_number:
print("TOO LOW!")
elif guess > random_number:
print("TOO HIGH!")
else:
print("YOU WON!!!!")
play_again = input("Do you want to play again? (y/n) ")
if play_again == "y":
random_number = random.randint(1,10) # numbers 1 - 10
guess = None
else:
print("Thank you for playing!")
break

Test 1

$ python3 guessingv2.py 
Pick a number from 1 to 10: 4
TOO LOW!
Pick a number from 1 to 10: 5
TOO LOW!
Pick a number from 1 to 10: 6
TOO LOW!
Pick a number from 1 to 10: 7
YOU WON!!!!
Do you want to play again? (y/n) n
Thank you for playing!

Test 2

$ python3 guessingv2.py 
Pick a number from 1 to 10: 1
TOO LOW!
Pick a number from 1 to 10: 2
TOO LOW!
Pick a number from 1 to 10: 3
TOO LOW!
Pick a number from 1 to 10: 4
TOO LOW!
Pick a number from 1 to 10: 5
TOO LOW!
Pick a number from 1 to 10: 6
TOO LOW!
Pick a number from 1 to 10: 7
YOU WON!!!!
Do you want to play again? (y/n) y
Pick a number from 1 to 10:

Test 3

$ python3 guessingv2.py 
Pick a number from 1 to 10: 3
TOO LOW!
Pick a number from 1 to 10: 4
TOO LOW!
Pick a number from 1 to 10: 5
TOO LOW!
Pick a number from 1 to 10: 6
TOO LOW!
Pick a number from 1 to 10: 7
TOO LOW!
Pick a number from 1 to 10: 8
YOU WON!!!!
Do you want to play again? (y/n) 8
Thank you for playing!

While everything else is same as previous version, we need to provide “True” to make sure this while loop is infinite. As we guess just like previously until “YOU WON!” is shown. After that, you will be asked to provide either “y” or “n” (or any other inputs). If “y” is given, the while loop will go continue until the point, “n” or any other inputs reached after “YOU WON!” is shown. Break means it will be ended indefinitely.

Combo Games Human vs. AI with selected round

1 Create a file named rps_with_loop-1.py

from random import randint
player_wins = 0
computer_wins = 0
winning_score = 3
while player_wins < winning_score and computer_wins < winning_score:
print(f"Player Score: {player_wins} Computer Score: {computer_wins}")
print("...rock...")
print("...paper...")
print("...scissors...")
player = input("(Enter your choice): ").lower()
if player == "quit" or player == "q":
break
random_num = randint(0, 2)
if (random_num == 0):
computer = "rock"
elif (random_num == 1):
computer = "paper"
else:
computer = "scissors"
print(f"The computer plays: {computer}")if player == computer:
print("It's a tie!")
elif player == "rock":
if computer == "paper":
print("Computer wins :( ")
computer_wins += 1
else:
print("Player wins!")
player_wins += 1
elif player == "paper":
if computer == "rock":
print("Player win!")
player_wins += 1
else:
print("Computer wins!")
computer_wins += 1
elif (player == "scissors"):
if (computer == "rock"):
print("Computer wins!")
computer_wins += 1
else:
print("You win!")
player_wins += 1
else:
print("Please enter a valid move!")
if player_wins > computer_wins:
print("CONGRATS, YOU WON!")
elif player_wins == computer_wins:
print("IT'S A TIE")
else:
print("OH NO :( THE COMPUTER WON...")

Test 1

$ python3 rps_with_loop-1.py 
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: paper
Computer wins :(
Player Score: 0 Computer Score: 1
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: paper
Computer wins :(
Player Score: 0 Computer Score: 2
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: scissors
Player wins!
Player Score: 1 Computer Score: 2
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: paper
Computer wins :(
OH NO :( THE COMPUTER WON...

Test 2

$ python3 rps_with_loop-1.py 
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: scissors
Player wins!
Player Score: 1 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: paper
Computer wins :(
Player Score: 1 Computer Score: 1
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: scissors
Player wins!
Player Score: 2 Computer Score: 1
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: paper
Computer wins :(
Player Score: 2 Computer Score: 2
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: scissors
Player wins!
CONGRATS, YOU WON!

Test 3

$ python3 rps_with_loop-1.py 
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: rock
It's a tie!
Player Score: 0 Computer Score: 0
...rock...
...paper...
...scissors...
(Enter your choice): rock
The computer plays: scissors
Player wins!
CONGRATS, YOU WON!

Here’s the most complicated one. So let’s break it down into details.

First and foremost, we setup player and computer both at 0 to start with. And the game ends as soon as one party reaches to 3.

There are 3 possible scenarios, computer wins, player wins or a tie. Tie is only when the game is still ongoing. Ultimately, we will reach to two possible outcomes, either computer or player wins.

In the while loop, we will make sure that the number of games played will always be less than what we set up.

Then first we ask player to input rock, paper or scissors. But we give player the option to quit with typing either “quit” or “q”.

Then we setup our computer with 3 random values from 0 to 2 with rock, paper or scissors. Whatever it is selected, print out that value as valid computer choice.

If statement, then, is brought to the table. If player’s choice = computer’s choice, then a tie is printed. Otherwise, player may have 3 possible scenarios, rock, paper and scissors. And for each choice, computer’s choice should be the other two choices. Like if player chooses rock, then computer reaches to scissors, player will win. On the contrary, if computer reaches to paper, then computer wins. And each time, player or computer wins, the number of wins is counted and added. If you don’t provide with one of three choices, then “Please enter a valid move!” is shown.

That’s it, play and practice your Python skills with this interesting game!

--

--

Paul Zhao
Paul Zhao Projects

Amazon Web Service Certified Solutions Architect Professional & Devops Engineer