The Wayback Machine - https://web.archive.org/web/20240921213434/https://www.geeksforgeeks.org/python-program-implement-rock-paper-scissor-game/
Open In App

Python program to implement Rock Paper Scissor game

Last Updated : 09 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 between Rock, paper, and scissors. After the 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.

Implementation: 

Python
import random

# Print multiline instruction
print('Winning rules of the game ROCK PAPER SCISSORS are:\n'
      + "Rock vs Paper -> Paper wins \n"
      + "Rock vs Scissors -> Rock wins \n"
      + "Paper vs Scissors -> Scissors wins \n")

while True:

    print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n")

    # Take the input from user
    choice = int(input("Enter your choice: "))

    # Looping until user enters valid input
    while choice > 3 or choice < 1:
        choice = int(input('Enter a valid choice please ☚: '))

    # 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 = 'Scissors'

    # Print user choice
    print('User choice is:', choice_name)
    print("Now it's Computer's Turn...")

    # Computer chooses randomly any number among 1, 2, and 3
    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 = 'Scissors'

    print("Computer choice is:", comp_choice_name)
    print(choice_name, 'vs', comp_choice_name)

    # Determine the winner
    if choice == comp_choice:
        result = "DRAW"
    elif (choice == 1 and comp_choice == 2) or (comp_choice == 1 and choice == 2):
        result = 'Paper'
    elif (choice == 1 and comp_choice == 3) or (comp_choice == 1 and choice == 3):
        result = 'Rock'
    elif (choice == 2 and comp_choice == 3) or (comp_choice == 2 and choice == 3):
        result = 'Scissors'

    # Print the result
    if result == "DRAW":
        print("<== It's a tie! ==>")
    elif result == choice_name:
        print("<== User wins! ==>")
    else:
        print("<== Computer wins! ==>")

    # Ask if the user wants to play again
    print("Do you want to play again? (Y/N)")
    ans = input().lower()
    if ans == 'n':
        break

# After coming out of the while loop, print thanks for playing
print("Thanks for playing!")

Output: 

Winning rules of the game ROCK PAPER SCISSORS are:
Rock vs Paper -> Paper wins
Rock vs Scissors -> Rock wins
Paper vs Scissors -> Scissors wins

Enter your choice
1 - Rock
2 - Paper
3 - Scissors

Enter your choice: 1
User choice is: Rock
Now it's Computer's Turn...
Computer choice is: Paper
Rock vs Paper
<== Computer wins! ==>
Do you want to play again? (Y/N)
y
Enter your choice
1 - Rock
2 - Paper
3 - Scissors

Enter your choice: 2
User choice is: Paper
Now it's Computer's Turn...
Computer choice is: Paper
Paper vs Paper
<== It's a tie! ==>
Do you want to play again? (Y/N)
n
Thanks for playing!

Code Explanation:

  1. The code starts by printing a message that states the rules of the game.
  2. The first line in the code prints “Rock vs paper->paper wins.”
  3. This is because if you have a rock, and you play against someone who has a piece of paper, the rock will beat the paper.
  4. The next line in the code prints “Rock vs scissor->Rock wins.”
  5. This is because if you have a rock, and you play against someone who has a scissors, the rock will beat the scissors.
  6. The last line in the code prints “paper vs scissor->scissor wins.”
  7. This is because if you have a piece of paper, and you play against someone who has a scissors, then the piece of paper will beat the scissors.
  8. The code will print the following output: Winning Rules of the Rock paper scissor game as follows: Rock vs paper->paper wins Rock vs scissor->Rock wins paper vs scissor->scissor wins
  9. The code starts by asking the user for a choice.
  10. The code then checks to see if the input is 1, 2, or 3.
  11. If it is not one of those values, the code sets the choice_name variable to ‘Rock’ if choice == 1, ‘paper’ if choice == 2, and ‘scissor’ if choice == 3.
  12. The next part of the code asks the user for their computer turn.
  13. The code uses a random number generator to choose between 1, 2, and 3.
  14. This value is stored in comp_choice_name.
  15. Next, the code loops until comp_choice equals choice.
  16. In each loop iteration, comp_choice will be randomly chosen from 1-3 and stored in comp_choice_name.
  17. Once comp_choice equals choice, this means that the computer has chosen rock as its turn!
  18. Finally, it prints out both choices so that everyone can see what happened (user choice is: Rock V/s paper; Computer choice is: Rock V/s scissor).
  19. The code will ask the user for a choice between rock, paper and scissors.
  20. Once the user enters their choice, the code will randomly choose one of those options as the computer’s turn.
  21. The code then prints out the chosen option and the user’s choice.
  22. Finally, it loops back to ask for another choice from the user.


Previous Article
Next Article

Similar Reads

Rock, Paper, Scissor game - Python Project
In this article, we will see how we can create a rock paper and scissor game using Tkinter. Rock paper scissor is a hand game usually played between two people, in which each player simultaneously forms one of the three shapes with an outstretched hand. These shapes are “rock”, “paper”, and “scissors”. Game Winner Conditions Let there be a Player w
15+ min read
PyQt5 - Rock Paper and Scissor Game
In this article we will see how we can create a rock paper and scissor game using PyQt5. Rock paper scissors is a hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock", "paper", and "scissors". Below is the representation of how the game will loo
13 min read
Rock Paper and Scissor Game Using Tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. Prerequisite: Tkinter, random In this articl
7 min read
Python | Program to implement Jumbled word game
Python is a multipurpose language and one can do literally anything with it. Python can also be used for game development. Let’s create a simple Jumbled word game without using any external game libraries like PyGame. Jumbled word game: Jumbled word is given to player, player has to rearrange the characters of the word to make a correct meaningful
7 min read
Python | Program to implement simple FLAMES game
Python is a multipurpose language and one can do literally anything with it. Python can also be used for game development. Let’s create a simple FLAMES game without using any external game libraries like PyGame. FLAMES is a popular game named after the acronym: Friends, Lovers, Affectionate, Marriage, Enemies, Sibling. This game does not accurately
6 min read
Counting Rock Samples | TCS Codevita 2020
John is a geologist, and he needs to count rock samples in order to send it to a chemical laboratory. He has a problem. The laboratory only accepts rock samples by a range of sizes in ppm (parts per million). John needs your help. Your task is to develop a program to get the number of rocks in each range accepted by the laboratory. Problem Statemen
5 min read
Python Program for word Guessing Game
Learn how to create a simple Python word-guessing game, where players attempt to guess a randomly selected word within a limited number of tries. Word guessing Game in PythonThis program is a simple word-guessing game where the user has to guess the characters in a randomly selected word within a limited number of attempts. The program provides fee
5 min read
Python program for Zip, Zap and Zoom game
Zip zap zoom is a fun game that requires its players to concentrate and participate actively. When this game is played with people, this is how it is played: Players stand in a circle, 6 feet away from each otherOne player starts the game by clap-pointing and saying zap to the person on its leftThis person now does the same to the person to its rig
3 min read
Python Program to Implement Stack Using Linked List
In Python, creating a stack using a linked list involves implementing a data structure where elements are added and removed in a last-in-first-out (LIFO) manner. This approach uses the concept of nodes interconnected by pointers, allowing efficient insertion and deletion operations. We are given a Linked list and our task is to create a stack using
4 min read
Python program to implement Half Subtractor
Prerequisite: Half Subtractor in Digital Logic Given two inputs of Half Adder A, B. The task is to implement the Half Subtractor circuit and Print output i.e Difference and Borrow of two inputs. The half subtractor is also a building block for subtracting two binary numbers. It has two inputs and two outputs. This circuit is used to subtract two si
2 min read
Python program to implement Half Adder
Prerequisite: Half Adder in Digital Logic Given two inputs of Half Adder A, B. The task is to implement the Half Adder circuit and Print output i.e sum and carry of two inputs. Half Adder: A half adder is a type of adder, an electronic circuit that performs the addition of numbers. The half adder is able to add two single binary digits and provide
5 min read
Python implementation of automatic Tic Tac Toe game using random number
Tic-tac-toe is a very popular game, so let's implement an automatic Tic-tac-toe game using Python. The game is automatically played by the program and hence, no user input is needed. Still, developing an automatic game will be lots of fun. Let's see how to do this. NumPy and random Python libraries are used to build this game. Instead of asking the
5 min read
Color game using Tkinter in Python
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this
4 min read
Hangman Game in Python
Hangman is a word game in which the computer will randomly select a word from the dictionary and the player has to guess it correctly in a given number of turns. The word to be guessed is represented by the row of stars. If the guessed letter is present in a word, the script will automatically be placed in the correct places. Rules to guess the wor
4 min read
Python | Catching the ball game
Python is a multipurpose language and can be used in almost every field of development. Python can also be used to develop different type of game. Let's try to develop a simple Catching the ball game using Python and TKinter.Game is very simple. There is one bar at the bottom of game window which can be moved left or right using the buttons that ar
6 min read
Python | Simple FLAMES game using Tkinter
Prerequisites: Introduction to TkinterProgram to implement simple FLAMES game Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and e
5 min read
21 Number game in Python
21, Bagram, or Twenty plus one is a game which progresses by counting up 1 to 21, with the player who calls "21" is eliminated. It can be played between any number of players. Implementation This is a simple 21 number game using Python programming language. The game illustrated here is between the player and the computer. There can be many variatio
11 min read
Python | PokÃĐmon Training Game
Problem : You are a PokÃĐmon trainer. Each PokÃĐmon has its own power, described by a positive integer value. As you travel, you watch PokÃĐmon and you catch each of them. After each catch, you have to display maximum and minimum powers of PokÃĐmon caught so far. You must have linear time complexity. So sorting won't help here. Try having minimum extra
1 min read
Mastermind Game using Python
Given the present generation's acquaintance with gaming and its highly demanded technology, many aspire to pursue the idea of developing and advancing it further. Eventually, everyone starts at the beginning. Mastermind is an old code-breaking game played by two players. The game goes back to the 19th century and can be played with paper and pencil
8 min read
Introduction to pyglet library for game development in Python
Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc on Windows, Mac OS and Linux. This library is created purely in Python and it supports many features like windowing, user interface event handling, Joysticks, OpenGL graphics, loading images, and videos, and playing sounds and music.
2 min read
Number Guessing Game in Python using Binary Search
In the number guessing game, the user selects a number within a defined range and then the program guesses the number. If the number guessed is wrong then the user tells the program that is the actual number is greater than the guessed number or not. Similarly, the program again guesses the number until the actual number is not guessed.Approach: Th
3 min read
Tic Tac Toe Game using PyQt5 in Python
In this article , we will see how we can create a Tic Tac Toe game using PyQt5. Tic-tac-toe, noughts, and crosses, or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. Below
5 min read
Create a Simple Two Player Game using Turtle in Python
Prerequisites: Turtle Programming in Python TurtleMove game is basically a luck-based game. In this game two-players (Red &amp; Blue), using their own turtle (object) play the game. How to play The game is played in the predefined grid having some boundaries. Both players move the turtle for a unit distance.Now both players flip the coin:if HEAD, t
4 min read
Snake Water Gun game using Python and C
Snake Water Gun is one of the famous two-player game played by many people. It is a hand game in which the player randomly chooses any of the three forms i.e. snake, water, and gun. Here, we are going to implement this game using python. This python project is to build a game for a single player that plays with the computer Following are the rules
8 min read
Tic Tac Toe game with GUI using tkinter in Python
Tic-tac-toe (American English), noughts and crosses (British English), or Xs and Os is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row is the winner. tkinter Python library is used to create the GUI.
15+ min read
Quote Guessing Game using Web Scraping in Python
Prerequisite: BeautifulSoup Installation In this article, we will scrape a quote and details of the author from this site http//quotes.toscrape.com using python framework called BeautifulSoup and develop a guessing game using different data structures and algorithm. The user will be given 4 chances to guess the author of a famous quote, In every ch
3 min read
Python - Cows and Bulls game
Cows and Bulls is a pen and paper code-breaking game usually played between 2 players. In this, a player tries to guess a secret code number chosen by the second player. The rules are as follows: A player will create a secret code, usually a 4-digit number. This number should have no repeated digits.Another player makes a guess (4 digit number) to
3 min read
Game of Craps in Python
In this article, we are going to discuss how to create the Game of Craps using Python. Rules of the game: Two dices are required to play and a player rolls two six-sided dice and adds the numbers rolled together.If on the first roll a player encounters a total of 7 or 11 the player automatically wins, and if the player rolls a total of 2, 3, or 12
5 min read
Python - MCQ Quiz Game using Tkinter
Prerequisite: Python GUI – tkinter Python provides a standard GUI framework Tkinter which is used to develop fast and easy GUI applications. Here we will be developing a simple multiple-choice quiz in python with GUI. We will be creating a multiple choice quiz in Python with Tkinter. First, we will create a library named Quiz in the directory of yo
6 min read
KBC game using Python
In this article, we will make a KBC game with almost all the features in CLI(Command Line Interface) mode using Python. Features of the game:Random questions every timeFour lifelinesAudience Poll50:50Double dipFlip the questionMade with the help of Matplotlib and NumPy libraries of python.Bar graph for audience poll lifelineRules to play the game:T
15 min read
Article Tags :
Practice Tags :