In this article, you will learn how to make a Rock Paper Scissors Python game in very simple coding in order to make our first ever game using python. Rock Paper Scissor is a game you might have played before. A pretty fun game isn’t it? We will create this game using python which pretty fun way to learn python, even if you are a beginner in coding or in python you can make this game very easily.
What Is Rock Paper Scissors?
Rock Paper Scissor is a game that you might play in your childhood. this is a simple hand game that two or more people can play together.
There are some rules if you are clearly unfamiliar with this game:
- Participants say “rock, paper, scissors”.
- And then simultaneously from their hand to form the fist(rock), palm facing downwards(paper)- , two fingers extended(scissor).
- Basically, rock smashes scissors.
- Scissor cuts paper.
- Paper covers rock.
What You Should Know Before Making Rock Paper Scissors Python Game
- A simple basic knowledge of python
Implementation of Rock Paper Scissors Python Game
Before taking a deep dive into the main code we need to implement the required modules, here we will import only one module named random module. this module needed to select a random number from a given number list.
from random import randint
Here we have imported randint from a random module. randint is a tool of a random module, this is used to get random numbers among the given number.
Looping The Game
We need to loop the code so we don’t need to rerun the code & the user can play again and again if the user wants to.
moves = ["rock","paper","scissors"]
while True:
computer = moves[randint(0,2)]
Array “moves” contains strings “rock”, ”paper”, ”scissor” and its indexes will be 0,1 and 2. at index 0 value will be rock, at index 1 value will be paper & at index 2 value will be scissors.
here we used a while loop with which code will be rerun until a condition becomes false , also moves[randint(0,2)] provides rock, paper, or scissor for the computer to move randomly.
Taking Input From User
In python, we can take input from users by writing the input() method. with this, we are requesting the user to give input.
player = input("rock, paper or scissors ? (or end the game)").lower()
if player == "end the game":
print("the game has ended...")
break
Users can give input such as rock or paper or scissors, or users want to continue playing the game or not. with the help of lower(), the method given input will be turned into lowercase alphabets.
if a player wants to quit the game then the user needs to write “end the game”. you can replace this text with some small integer like “end”. a break will simply terminate the rest of the code and the game will get ended.
Declaration Of Winner
If else conditions will determine the winner, where if player and computer select the same choice or move then the game will be tied.
elif player == computer:
print("Tie!!")
elif player == "rock":
if computer == "paper":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
elif player == "paper":
if computer == "scissors":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
elif player == "scissors":
if computer == "rock":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
else:
print("given input is invalid...")
Otherwise player selects one move and the computer selects other moves then only one condition will be checked and prints the result other conditions won’t be checked after that, which means in the case of if, elif, and else conditions only one condition needs to be true.
Let’s say the player chooses paper and if the computer chooses paper then it will be tied, else if the computer chooses scissors then the computer will win, and if the computer chooses rock then the computer will lose.
if the user doesn’t provide proper input or other than rock, paper, or scissors then it will print “given input is invalid”.
Full Source Code Of Rock Paper Scissors Python Game
#rock,paper,scissors game for begineers
from random import randint
#moves for the playes
moves = ["rock","paper","scissors"]
while True:
computer = moves[randint(0,2)]
player = input("rock, paer or scissors ? (or end the game) pape").lower()
if player == "end the game":
print("the game has ended...")
break
elif player == computer:
print("Tie!!")
elif player == "rock":
if computer == "paper":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
elif player == "paper":
if computer == "scissors":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
elif player == "scissors":
if computer == "rock":
print("you lose!", computer, "beats", player)
else:
print("you win!", player, "beats", computer)
else:
print("given input is invalid...")
Output
PS C:\Users\mypc> & C:/Users/mypc/AppData/Local/Programs/Python/Python310/python.exe c:/Users/mypc/Rock,Paper,Scissors.py
rock, paper or scissors ? (or end the game) rock
Tie!!
rock, paper or scissors ? (or end the game) paper
Tie!!
rock, paper or scissors ? (or end the game) rock
you win! rock beats scissors
rock, paper or scissors ? (or end the game) scissors
you win! scissors beats paper
rock, paper or scissors ? (or end the game) rock
you lose! paper beats rock
rock, paper or scissors ? (or end the game) end the game
the game has ended...
PS C:\Users\mypc>
And Boom… you made your first ever game in python, this is the simplest and most straightforward code possible in python in order to create a rock paper scissors python game.
and this is how you can make a rock paper scissors python game.