Python program to implement Rock Paper Scissor game

Python program to implement Rock Paper Scissor game Difficulty Level : Easy Last Updated : 30 Jan, 2022 Python is a multipurpose language...

Python program to implement Rock Paper Scissor game

  • Difficulty Level : Easy
  • Last Updated : 30 Jan, 2022

Python is a multipurpose language and one can do anything with it. Python can also be used for game development. Let’s create a simple command-line Rock-Paper-Scissor game without using any external game libraries like PyGame.
In this game, the user gets the first chance to pick the option among Rock, paper and scissors. After that computer select from the remaining two choices(randomly), the winner is decided as per the rules.

Winning Rules as follows :

Rock vs paper-> paper wins
Rock vs scissor-> Rock wins
paper vs scissor-> scissor wins.

In this game, randint() inbuilt function is used for generating random integer values within the given range.
Below is the implementation : 

# import random module
import random
 
# Print multiline instruction
# performstring concatenation of string
print("Winning Rules of the Rock paper scissor game as follows: \n"
                                +"Rock vs paper->paper wins \n"
                                + "Rock vs scissor->Rock wins \n"
                                +"paper vs scissor->scissor wins \n")
 
while True:
    print("Enter choice \n 1 for Rock, \n 2 for paper, and \n 3 for scissor \n")
     
    # take the input from user
    choice = int(input("User turn: "))
 
    # OR is the short-circuit operator
    # if any one of the condition is true
    # then it return True value
     
    # looping until user enter invalid input
    while choice > 3 or choice < 1:
        choice = int(input("enter valid input: "))
         
 
    # initialize value of choice_name variable
    # corresponding to the choice value
    if choice == 1:
        choice_name = 'Rock'
    elif choice == 2:
        choice_name = 'paper'
    else:
        choice_name = 'scissor'
         
    # print user choice
    print("user choice is: " + choice_name)
    print("\nNow its computer turn.......")
 
    # Computer chooses randomly any number
    # among 1 , 2 and 3. Using randint method
    # of random module
    comp_choice = random.randint(1, 3)
     
    # looping until comp_choice value
    # is equal to the choice value
    while comp_choice == choice:
        comp_choice = random.randint(1, 3)
 
    # initialize value of comp_choice_name
    # variable corresponding to the choice value
    if comp_choice == 1:
        comp_choice_name = 'Rock'
    elif comp_choice == 2:
        comp_choice_name = 'paper'
    else:
        comp_choice_name = 'scissor'
         
    print("Computer choice is: " + comp_choice_name)
 
    print(choice_name + " V/s " + comp_choice_name)
 
    # condition for winning
    if((choice == 1 and comp_choice == 2) or
      (choice == 2 and comp_choice ==1 )):
        print("paper wins => ", end = "")
        result = "paper"
         
    elif((choice == 1 and comp_choice == 3) or
        (choice == 3 and comp_choice == 1)):
        print("Rock wins =>", end = "")
        result = "Rock"
    else:
        print("scissor wins =>", end = "")
        result = "scissor"
 
    # Printing either user or computer wins
    if result == choice_name:
        print("<== User wins ==>")
    else:
        print("<== Computer wins ==>")
         
    print("Do you want to play again? (Y/N)")
    ans = input()
 
 
    # if user input n or N then condition is True
    if ans == 'n' or ans == 'N':
        break
     
# after coming out of the while loop
# we print thanks for playing
print("\nThanks for playing")

Output : 

winning Rules of the Rock paper and scissor game as follows:
rock vs paper->paper wins 
rock vs scissors->rock wins 
paper vs scissors->scissors wins 

Enter choice 
 1. Rock 
 2. paper 
 3. scissor 

User turn: 1
User choice is: Rock

Now its computer turn.......

computer choice is: paper
Rock V/s paper
paper wins =>computer wins
do you want to play again?
N

COMMENTS

Name

[ Download ] Bhuj The Pride of India Full Movie 480p & 720p,1,[DOWNLOAD] Heropanti 2 Movie Download Free in Hindi 720p 1080p,1,10 Steps To Become A Hacker (An Ethical Hacker),1,1080P,1,Allen biology module,1,Allen chemistry module,1,Allen physics module,1,Bhool Bhulaiyaa 2 Download in Hindi– 720P,1,Download [KGF-2 South Blockbuster Movie in Hindi Dubbed] Free,1,Download Tere Bin Laden (2010) 480p | 720p HD,1,Download[RRR-2022] south blockbuster movie download in hindi dubbed in hd,1,Free Ethical Hacking Course for Beginners,1,Heropanti 2 Songs Download,1,How QR Codes Work and What Makes Them Dangerous—a Computer Scientist Explains,1,How to do Carding,1,How To Hack/Unlock Android Pattern,1,Instagram King Mod,1,Instagram Mod apk,1,Instagram MOD APK 234.0.0.19.113 (Instathunder),1,Introducing Windows 11,1,Is Warp Drive real?,1,PW app apk,1,Samrat Prithviraj Movie Download Downloadhub 480p|,1,Wifi Hacking,1,
ltr
item
CyberPedia: Python program to implement Rock Paper Scissor game
Python program to implement Rock Paper Scissor game
CyberPedia
https://gofindly.blogspot.com/2022/05/python-program-to-implement-rock-paper.html
https://gofindly.blogspot.com/
https://gofindly.blogspot.com/
https://gofindly.blogspot.com/2022/05/python-program-to-implement-rock-paper.html
true
8718369367284999788
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content