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

Brand NameDiscount InformationCoupon Codes Link
Educative.io20% discount on Educative courses and plans
W3Schools20% discount on W3Schools courses
KodeKloud10% discount on KodeKloud courses and plans
GeeksforGeeks30% discount on GeeksforGeeks courses
Target Test Prep20% discount on Target Test Prep
Coding Ninjas₹5000 discount on Coding Ninjas courses
Skillshare40% discount on Skillshare
DataCamp50% discount on DataCamp
365 Data Science57% discount on 365 Data Science Plans
Get SmarterFlat 20% discount on Get Smarter courses
SmartKeedaFlat 40% discount on SmartKeeda courses
StackSocial20% discount on StackSocial 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

Auto Text Effect Animation Using Html CSS & JavaScript Introduction Hello friends, welcome to today’s new blog post. Today we have created …

Windows 12 Notepad Using Python Introduction: In this article, we will create a Windows 12 Notepad using Python. If you are a …

Animated Search Bar using Html CSS And JavaScript Introduction Hello friends, all of you developers are welcome to today’s beautiful blog post. …

Best Quiz Game Using HTML CSS And JavaScript Introduction Hello coders, welcome to another new blog. Today in this article we’ll learn …

Tower Blocks Game Using HTML CSS And JavaScript Introduction Hello coders, welcome to another new blog. Today in this blog we’ll learn …

Typing Speed Test Game using Python Introduction: In this article, we will create a Typing Speed Test Game using Python and Tkinter. …

Get Huge Discounts
More Python Projects