Rock Paper Scissor Game using Python with Source Code
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:
Find More Projects
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …