Rock, Paper, and Scissors Game Using Python Programming
Make an easy game with just 20 lines of Python code
Python is a super interesting and very easy-to-understand programming language. You can make cool projects with just a few lines of code. To brush up on your skill in Python, you need to make projects. Whether you are a pro or a newbie doesn’t matter. You have to make projects to step up. But at the beginning of the learning phase, you can’t simply jump on a tough project. It will ruin your experience. When I was learning, this is the first project I built using Python. Yes, it’s the game we play we play when we are kids. Let’s build Rock, Paper, Scissors using Python.
The video tutorial of this article is here:
Rules of the Game
In the Rock, Paper, and Scissors game, only two players can participate. Each player has to pick the one(Rock, Paper, or Scissors) randomly at a time.
Rock will smash the scissors.
Paper will cover the Rock.
Scissors will cut the paper.
Let suppose you and your best friend are playing this game. You selected Paper and Your best friend selected Rock. Then you will be the winner of the game because the paper will cover the rock. If your best friend selected paper then it will be a tie. And if the opponent selected scissors then in this case, you will lose because the scissors will cut the paper. The same conditions will go for other cases too.
You have to show the sign of the Rock, Paper, and Scissors randomly at a time using your hand. For rock you can use your fist, for the paper you can use your palm, For scissors, you can use the victory symbol using the fingers.
Execute the game
Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad
In this scenario, our opponent is our computer. So, we have to write a program for the computer. The computer has to choose between rock, paper, and scissors randomly.
Step 1:
import randommy_action = input("rock, paper or scissors?")
opponent_action = random.choice(["rock", "paper", "scissors"])
We are going to use Python’s random library. In this game, we can choose only one from the available options Rock, Paper, or Scissors. We are taking our action as input. And with the help of a random library, we let the computer choose between the three options randomly.
Step 2:
print(f"My choice is: {my_action}")
print(f"Opponent Choose: {opponent_action}")
In this step, we simply print our action and the opponent's action.
Step 3:
if my_action == opponent_action:
print(f"It's a tie. Both of you selected {my_action}")elif my_action == "rock":
if opponent_action =="scissors":
print("Rock smashes scissors.. I win!!")
else:
print("Opponent Win!! paper covers rock")elif my_action == "paper":
if opponent_action == "rock":
print("I win!!! paper covers rock")
else:
print("Opponent Win!! scissors cut paper")elif my_action == "scissors":
if opponent_action == "paper":
print("I Win!!! Scissors cut paper")
else:
print("Opponent Win.. Rock smashes scissors")
Here are the logics we are going to use.
First, If my action and opponent’s action(computer) is equal then it’s will be a tie.
Second, If I choose “rock” and set it in my action. The computer can randomly pick one out of three options(Rock, paper, and scissors).
If the computer picks rock then it is the first case, and it will be a tie. If the computer chooses scissors then I will be the winner of the round because the rock will smash the scissors. But if the opponent's choice is paper then the opponent will be declared as the winner, because the paper will cover the rock.
The same logic goes for the paper and scissors conditions too.
If you run this code in your terminal or command prompt window, the output of the code will be something like this.
Yessss..!! It’s working perfectly. It’s a fun game. You must try this.
Well, That’s it.
If this article sounds informative then make sure to follow and share it with your geek community.
Here are more Python projects for practice!
Make a Calculator using Python and Tkinter
YouTube Video Downloader using Python and Tkinter
Text to Speech Conversion using Python with gTTS
Capture and Process Video Footage from a Webcam using OpenCV Python
Random Password Generator using Python Programming
Happy coding..!!
Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build react native projects and currently working on Python Django. Feel free to contact me (freelance.rohit7@gmail.com)