Rock Paper Scissor Game using Python with Source Code
![](https://codewithcurious.com/wp-content/uploads/2022/11/Handwritten-Notes-18-1024x576.png)
Introduction:
Python is a high-level programming language and it is used to develop
games as well. In this, we have created a command-line Rock Paper Scissors
game. According to the rules, the user gets the chance to pick the option
first, and then the computer’s choice will be generated randomly and so the
winner will get declared.
In this article, we’ll how to make a rock-paper-scissor game using python. rock-paper-scissor is a game that is played by two people. which each player simultaneously forms one of three shapes with their hand . These shapes are “rock”, “paper”, and “scissors”.
Source Code :
#import random module with 'r' as its alias
import random as r
print("WELCOME!!")
#Giving the information to user
print("Enter 1 for choosing Rock \n Enter 2 for choosing Paper \n Enter 3 for choosing Scissors\n")
while True:
#take the input from user
user_ch=int(input("Enter your choice"))
#initializing the value of user_ch variable
if(user_ch==1):
print("You have chose Rock!\n") #priting the user choice accordingly elif(user_ch==2):
print("You have chose Paper!\n")
else:
print("You have chose Scissors!\n")
print("Now it's turn for computer's Choice\n")
#initializing the value of cp_ch variable
#computer will randomly choose any number among 1,2,3
cp_ch=r.randint(1,3) #Using randint method of random module
if(cp_ch==1):
print("Computer's choice: Rock!\n") #printing the random computer'schoice
elif(cp_ch==2):
print("Computer's choice: Paper!\n")
else:
print("Computer's choice: Scissors!\n")
#Conditions to win
print("Now it's Time to Play")
if((user_ch==1 and cp_ch==2) or (user_ch==1 and cp_ch==3) or(user_ch==3 and cp_ch==2)):
print("User wins!!")
elif((user_ch==2 and cp_ch==1) or (user_ch==2 and cp_ch==3)or
(user_ch==3 and cp_ch==1)):
print("Computer wins!!")
#checking for draw
elif(user_ch==cp_ch):
print("It's a tie!")
#asking user to play more
print("Do you want to play again(Y/N)?")
a=input()
#if input is N then the condition will be true and loop continues
if(a=='N'):
break
print("Thanks for playing!")
Explanation:
In the beginning, we will import the “random” module with the help of the “import” keyword. For the next part, we will provide suitable information to the user to understand the rules properly. → To make it more user-friendly we have given an option to the user whether they have to continue the game or not, for that we have declared a while loop in which the game part will exist. → In the next step, a variable named user_ch is used to store the input from the user and after that, we have initialized the value for the variable with the help of the if condition. → After doing so, now it’s the computer’s turn to choose any of the three, that we have initialized one more variable named cp_ch which will store the computer’s choice, and since the choice will generate randomly so we will use random.randint() method from the random module (randint(a,b) method will generate a random number between “a” and “b”), with the same process, will initialize the value for this as well.
Output:
![](https://codewithcurious.com/wp-content/uploads/2022/11/Screenshot-2022-11-10-at-17.00.58-300x199.png)
![](https://codewithcurious.com/wp-content/uploads/2022/11/Screenshot-2022-11-10-at-17.00.45.png)
![](https://codewithcurious.com/wp-content/uploads/2022/11/Screenshot-2022-11-10-at-17.00.58.png)
Find More Projects
Personal Finance Manager using java swing with complete source code Introduction: The Personal Finance Manager is a Java-based application designed to help …
Gym Website Using HTML, CSS, and JavaScript Introduction Hello friends, all of you are welcome to today’s beautiful project. Today we have …
Jarvis Chatbot Using HTML Jarvis Chatbot Using HTML Jarvis Chatbot Using HTML CSS & javaScript Introduction Hello friends, hope you all are …
movie Website using HTML, CSS, and JavaScript Introduction Hello friends my name is Gautam and you all are welcome to today’s beautiful …
Digital Clock with HTML, CSS, and JavaScript Introduction Hello friends, my name is Gautam and you are all welcome to today’s new …
Calendar Using HTML, CSS, and JavaScript (Source Code) Introduction Hello friends my name is Gautam and you all are welcome to today’s …