Rock Paper Scissor Game Using Python

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”.

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:

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

library management system using python with source code using Python GUI Tkinter (Graphical User Interface) How to Run the code: Introduction:  Imagine …

Space Shooter Game Using Python with source code Overview: A space shooter game typically involves controlling a spaceship to navigate through space …

Hotel Management System Using Python with source code Introduction: Welcome to our blog post introducing a helpful tool for hotels: the Tkinter-based …

Student Management System Using Python Introduction: The Student Management System is a comprehensive software solution designed to streamline the process of managing …

Billing Management System using Python introduction: The Billing software using python is a simple yet effective Python application designed to facilitate the …

Medical Management System using Python with Complete Source code [By using Flask Framework] Introduction Hospital Management System The Hospital Management System is …

More Python Projects
Get Huge Discounts

All Coding Handwritten Notes

Browse Handwritten Notes