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 :
Get Discount on Top Educational Courses
#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
URL Shortener Using Python Django Introduction: Long URLs can be shortened into short, shareable links with the help of the URL Shortener …
User Authentication System Using Python Django Introduction: The implementation of safe and adaptable user authentication in Django is the main goal of …
The E-Learning System using Java with a Graphical User Interface (GUI) Introduction The E-Learning System is developed using Java (with a Graphical …
Weather App Using Python Django Introduction: When a user enters the name of a city, the Weather App retrieves current weather information. …
Quiz App Using Python Django Introduction: Users can take quizzes in a variety of subjects, see their results, and monitor their progress …
resume screener in python using python introduction The hiring process often begins with reviewing numerous resumes to filter out the most suitable …