×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Rock Paper Scissors Game in Python

Here, we are implementing a Python program to implement rock paper scissors game.
Submitted by Abhinav Gangrade, on July 12, 2020

Module used:
In this script, we will use python's inbuilt module named random.

random module:
random is a Python inbuilt module which will help us to select any random element from a list or an array of elements. In this, we will use this module for selecting randomly the choice on behalf of the computer.

The rules of the games as:

Rock will be at 1.
Paper will be at 2.
Scissor will be at 3.

Rock vs Scissor => Rock wins.
Rock vs Paper => Paper wins.
Paper vs Scissor=> Scissor wins.

Program:

# importing the module
import random

Name=input("Enter your name: ")
print("The Rules of the game are as follow:\n"
      "1) Rock at 1.\n2) Paper at 2."
      "\n3) Scissor at 3."
      "\nRock vs Paper=> Paper wins"
      "\nRock vs Scissor=>Rock wins"
      "\nPaper vs Scissor=> Scissor wins")

# entering into the loop if the 
# user wants to play it again and again
while True:
    print("Enter the choice:\n"
          "1) Rock\n"
          "2) Paper\n"
          "3) Scissor")
    
    # the user turn
    choice=int(input("User turn:"))
    
    # if we put an invalid choice then we will
    # again take the input untill we put the right
    # one
    while choice<1 or choice>3:
        choice=int(input("Invalid Choice Enter Again"))
    if choice==1:
        user_choice="Rock"
    if choice==2:
        user_choice="Paper"
    if choice==3:
        user_choice="Scissor"
    print("User choice is "+user_choice)
    
    # users turn
    print("Computers turn")
    
    # using random for computers choice
    # using randint function to select random
    # from 1 to 3
    computer_choice=random.randint(1,3)
    
    # if the user choice is same as computer choice then
    # again we will take the choice untill.
    while choice==computer_choice:
        computer_choice=random.randint(1,3)
    if computer_choice==1:
        comp_choice="Rock"
    if computer_choice==2:
        comp_choice="Paper"
    if computer_choice==3:
        comp_choice="Scissor"
    print("Computer choice is "+comp_choice)
    # the condition to win
    # the rock winning condition
    if (choice==1 and computer_choice==3) or (choice==3 and computer_choice==1):
        print("Rock Wins")
        result="Rock"
    elif (choice==1 and computer_choice==2) or (choice==2 and computer_choice==1):
        print("Paper Wins")
        result="Paper"
    else:
        print("Scissor Wins")
        result="Scissor"
    if result==user_choice:
        print(f"{Name} wins")
    else:
        print("Computer wins")
    c=input("If you want to Continue(Y/N)")
    if c=="n" or c=="N":
        break

Output:

Enter your name: Abhinav
The Rules of the game are as follow:
1) Rock at 1.
2) Paper at 2.
3) Scissor at 3.
Rock vs Paper=> Paper wins
Rock vs Scissor=>Rock wins
Paper vs Scissor=> Scissor wins
Enter the choice:
1) Rock
2) Paper
3) Scissor
User turn:2
User choice is Paper
Computers turn
Computer choice is Scissor
Scissor Wins
Computer wins
If you want to Continue(Y/N)y
Enter the choice:
1) Rock
2) Paper
3) Scissor
User turn:1
User choice is Rock
Computers turn
Computer choice is Scissor
Rock Wins
Abhinav wins
If you want to Continue(Y/N)n
Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.