Rock Paper Scissors Game
Abstract
Rock, Paper, Scissors is a hand game played between two people. The game is played by two people using their hands. The players count to three in unison and simultaneously “throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the hand signals thrown by the players. The rules of the game are:
- Rock wins against scissors.
- Scissors win against paper.
- Paper wins against rock.
- If both players throw the same hand signal, there is no winner and the game is played again.
We are going to make a simple rock paper scissors game using python. This is a beginner level project. You can find the code for this project on my GitHub. In this project, we will use the random module to generate random numbers and then we will use if-else statements to check the user input and computer generated number and then we will decide the winner. You are against the computer in this game. The computer will randomly choose between rock, paper and scissors. You will be asked to choose between rock, paper and scissors. The computer will then check your choice against its choice and decide the winner. The game will continue until you decide to quit.
Prerequisites
- Python 3.6 or above
- A code editor or IDE
Getting Started
Create a Project
- Create a new folder and name it
rockpaperscissorsgame
rockpaperscissorsgame
. - Open the folder in your code editor.
- Create a new file and name it
rockpaperscissors.py
rockpaperscissors.py
. - Copy the code given at the beginning of this article and paste it in the
rockpaperscissors.py
rockpaperscissors.py
file.
Write the Code
- Copy and paste the following code in the
rockpaperscissors.py
rockpaperscissors.py
file.
⚙️ Rock Paper Scissors
# Rock, Paper, Scissors Game
# Import Libraries
import random
# Creating a list of options
options = ["ROCK", "PAPER", "SCISSORS"]
# Creating a function to play the game
def play():
# Getting the user's choice
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
# Getting the computer's choice
computer_choice = random.choice(options)
# Checking if the user's choice is valid
while user_choice not in options:
user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
# Checking the user's choice against the computer's choice
if user_choice.upper() == computer_choice.upper():
print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
print(f"Computer chose {computer_choice}. You win!")
else:
print(f"Computer chose {computer_choice}. You lose!")
# Playing the game
while True:
play()
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() != "y":
break
print("Thanks for playing!")
# Rock, Paper, Scissors Game
# Import Libraries
import random
# Creating a list of options
options = ["ROCK", "PAPER", "SCISSORS"]
# Creating a function to play the game
def play():
# Getting the user's choice
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
# Getting the computer's choice
computer_choice = random.choice(options)
# Checking if the user's choice is valid
while user_choice not in options:
user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
# Checking the user's choice against the computer's choice
if user_choice.upper() == computer_choice.upper():
print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
print(f"Computer chose {computer_choice}. You win!")
else:
print(f"Computer chose {computer_choice}. You lose!")
# Playing the game
while True:
play()
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() != "y":
break
print("Thanks for playing!")
- Save the File.
- Open the terminal and navigate to the
rockpaperscissorsgame
rockpaperscissorsgame
folder.
C:\Users\username\PythonCentralHub\projects\beginners\rockpaperscissorsgame> python rockpaperscissors.py
Choose Rock, Paper or Scissors: Rock
Computer chose SCISSORS. You win!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: paper
Computer chose PAPER. It's a tie!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: scissors
Computer chose SCISSORS. It's a tie!
Do you want to play again? (y/n): n
Thanks for playing!
C:\Users\username\PythonCentralHub\projects\beginners\rockpaperscissorsgame> python rockpaperscissors.py
Choose Rock, Paper or Scissors: Rock
Computer chose SCISSORS. You win!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: paper
Computer chose PAPER. It's a tie!
Do you want to play again? (y/n): y
Choose Rock, Paper or Scissors: scissors
Computer chose SCISSORS. It's a tie!
Do you want to play again? (y/n): n
Thanks for playing!
- You can see that the game is working as expected. You can play the game as many times as you want. You can also quit the game whenever you want.
Explanation
- We first import the
random
random
module. We will use this module to generate random numbers.
import random
import random
- We create a list of options. The computer will randomly choose from this list.
options = ["ROCK", "PAPER", "SCISSORS"]
options = ["ROCK", "PAPER", "SCISSORS"]
- We create a function to play the game. This function will be called whenever we want to play the game.
def play():
def play():
- We get the user’s choice. We use the
input()
input()
function to get the user’s choice. We convert the user’s choice to uppercase using theupper()
upper()
function. We store the user’s choice in theuser_choice
user_choice
variable.
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
user_choice = input("Choose Rock, Paper or Scissors: ").upper()
- We get the computer’s choice. We use the
random.choice()
random.choice()
function to get the computer’s choice. We pass theoptions
options
list as an argument to therandom.choice()
random.choice()
function. We store the computer’s choice in thecomputer_choice
computer_choice
variable.
computer_choice = random.choice(options)
computer_choice = random.choice(options)
- We check if the user’s choice is valid. We use the
while
while
loop to check if the user’s choice is valid. We check if the user’s choice is in theoptions
options
list. If the user’s choice is not in theoptions
options
list, we ask the user to enter a valid choice. We convert the user’s choice to uppercase using theupper()
upper()
function. We store the user’s choice in theuser_choice
user_choice
variable.
while user_choice not in options:
user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
while user_choice not in options:
user_choice = input("Invalid input. Choose Rock, Paper or Scissors: ").upper()
- We check the user’s choice against the computer’s choice. We use the
if-elif-else
if-elif-else
statements to check the user’s choice against the computer’s choice. We convert the user’s choice to uppercase using theupper()
upper()
function. We convert the computer’s choice to uppercase using theupper()
upper()
function. If the user’s choice is equal to the computer’s choice, we print that it’s a tie. If the user’s choice is rock and the computer’s choice is scissors, we print that the user wins. If the user’s choice is paper and the computer’s choice is rock, we print that the user wins. If the user’s choice is scissors and the computer’s choice is paper, we print that the user wins. If the user’s choice is not equal to the computer’s choice, we print that the user loses.
if user_choice.upper() == computer_choice.upper():
print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
print(f"Computer chose {computer_choice}. You win!")
else:
print(f"Computer chose {computer_choice}. You lose!")
if user_choice.upper() == computer_choice.upper():
print(f"Computer chose {computer_choice}. It's a tie!")
elif user_choice.upper() == "ROCK" and computer_choice.upper() == "SCISSORS":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "PAPER" and computer_choice.upper() == "ROCK":
print(f"Computer chose {computer_choice}. You win!")
elif user_choice.upper() == "SCISSORS" and computer_choice.upper() == "PAPER":
print(f"Computer chose {computer_choice}. You win!")
else:
print(f"Computer chose {computer_choice}. You lose!")
- We play the game. We use the
while
while
loop to play the game. We call theplay()
play()
function to play the game. We ask the user if they want to play again. If the user entersy
y
, we play the game again. If the user entersn
n
, we break out of thewhile
while
loop.
while True:
play()
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() != "y":
break
while True:
play()
play_again = input("Do you want to play again? (y/n): ")
if play_again.lower() != "y":
break
- We print a message to thank the user for playing the game.
print("Thanks for playing!")
print("Thanks for playing!")
Next Steps
Congratulations on completing this project. You can now move on to the next project where we will make a simple calculator using python. You can find the code for this project on my GitHub. If you like this article, please share it with your friends and colleagues. If you have any questions or suggestions, please let me know in the comments below.
There are some suggestions:
- Add a score counter to keep track of the score.
- Add a timer to limit the time for each round.
- Add a GUI to make the game more interactive.
- Add more options to make the game more interesting.
- Add a database to store the scores.
- Add a leaderboard to display the top scores.
- Add a multiplayer mode to play with friends.
- Add a chat feature to chat with friends while playing the game.
- Add a voice assistant to make the game more interactive.
- Add a virtual assistant to play the game with you.
Conclusion
In this article, we learned how to make a simple rock paper scissors game using python. This is a beginner level project. You can find the code for this project on my GitHub. In this project, we used the random module to generate random numbers and then we used if-else statements to check the user input and computer generated number and then we decided the winner. You can create more projects using python. You can find more projects on Python Central Hub. If you have any questions or suggestions, please let me know in the comments below.
Was this page helpful?
Let us know how we did