By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > Python > How To Make Rock Paper Scissors Python Game 2022
Python

How To Make Rock Paper Scissors Python Game 2022

Admin
Last updated: 2022/11/26 at 4:36 AM
Admin
Share
7 Min Read
Rock Paper Scissors Python

In this article, you will learn how to make a Rock Paper Scissors Python game in very simple coding in order to make our first ever game using python. Rock Paper Scissor is a game you might have played before. A pretty fun game isn’t it? We will create this game using python which pretty fun way to learn python, even if you are a beginner in coding or in python you can make this game very easily.

Contents
What Is Rock Paper Scissors?What You Should Know Before Making Rock Paper Scissors Python GameImplementation of Rock Paper Scissors Python GameLooping The GameTaking Input From UserDeclaration Of WinnerFull Source Code Of Rock Paper Scissors Python GameYou may also like:

What Is Rock Paper Scissors?

Rock Paper Scissor is a game that you might play in your childhood. this is a simple hand game that two or more people can play together.

There are some rules if you are clearly unfamiliar with this game:

  • Participants say “rock, paper, scissors”.
  • And then simultaneously from their hand to form the fist(rock), palm facing downwards(paper)- , two fingers extended(scissor).
  • Basically, rock smashes scissors.
  • Scissor cuts paper.
  • Paper covers rock.
Rock Paper Scissors Python Game
Image by EsquireME

What You Should Know Before Making Rock Paper Scissors Python Game

  • A simple basic knowledge of python

Implementation of Rock Paper Scissors Python Game

Before taking a deep dive into the main code we need to implement the required modules, here we will import only one module named random module. this module needed to select a random number from a given number list.

from random import randint

Here we have imported randint from a random module. randint is a tool of a random module, this is used to get random numbers among the given number.

Looping The Game

We need to loop the code so we don’t need to rerun the code & the user can play again and again if the user wants to.

moves = ["rock","paper","scissors"]
while True:
    computer = moves[randint(0,2)]

Array “moves” contains strings “rock”, ”paper”, ”scissor” and its indexes will be 0,1 and 2. at index 0 value will be rock, at index 1 value will be paper & at index 2 value will be scissors.

here we used a while loop with which code will be rerun until a condition becomes false , also moves[randint(0,2)] provides rock, paper, or scissor for the computer to move randomly.

Taking Input From User

In python, we can take input from users by writing the input() method. with this, we are requesting the user to give input.

player = input("rock, paper or scissors ? (or end the game)").lower()
 if player == "end the game":
        print("the game has ended...")
        break

Users can give input such as rock or paper or scissors, or users want to continue playing the game or not. with the help of lower(), the method given input will be turned into lowercase alphabets.

if a player wants to quit the game then the user needs to write “end the game”. you can replace this text with some small integer like “end”. a break will simply terminate the rest of the code and the game will get ended.

Declaration Of Winner

If else conditions will determine the winner, where if player and computer select the same choice or move then the game will be tied.

elif player == computer:
        print("Tie!!")
    
    elif player == "rock":
        if computer == "paper":
            print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)

    elif player == "paper":
        if computer == "scissors": 
             print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)

    elif player == "scissors":
        if computer == "rock": 
             print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)
    
    else:
        print("given input is invalid...")

Otherwise player selects one move and the computer selects other moves then only one condition will be checked and prints the result other conditions won’t be checked after that, which means in the case of if, elif, and else conditions only one condition needs to be true.

Let’s say the player chooses paper and if the computer chooses paper then it will be tied, else if the computer chooses scissors then the computer will win, and if the computer chooses rock then the computer will lose.

if the user doesn’t provide proper input or other than rock, paper, or scissors then it will print “given input is invalid”.

Full Source Code Of Rock Paper Scissors Python Game

#rock,paper,scissors game for begineers
from random import randint

#moves for the playes
moves = ["rock","paper","scissors"]
while True:
    computer = moves[randint(0,2)]
    player = input("rock, paer or scissors ? (or end the game)  pape").lower()
    
    if player == "end the game":
        print("the game has ended...")
        break

    elif player == computer:
        print("Tie!!")
    
    elif player == "rock":
        if computer == "paper":
            print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)

    elif player == "paper":
        if computer == "scissors": 
             print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)

    elif player == "scissors":
        if computer == "rock": 
             print("you lose!", computer, "beats", player)
        
        else:
            print("you win!", player, "beats", computer)
    
    else:
        print("given input is invalid...")

Output

PS C:\Users\mypc> & C:/Users/mypc/AppData/Local/Programs/Python/Python310/python.exe c:/Users/mypc/Rock,Paper,Scissors.py
rock, paper or scissors ? (or end the game)  rock
Tie!!
rock, paper or scissors ? (or end the game)  paper
Tie!!
rock, paper or scissors ? (or end the game)  rock
you win! rock beats scissors
rock, paper or scissors ? (or end the game)  scissors
you win! scissors beats paper
rock, paper or scissors ? (or end the game)  rock
you lose! paper beats rock
rock, paper or scissors ? (or end the game)  end the game
the game has ended...
PS C:\Users\mypc>

And Boom… you made your first ever game in python, this is the simplest and most straightforward code possible in python in order to create a rock paper scissors python game.

and this is how you can make a rock paper scissors python game.

You may also like:

  • Convert Images Into Pencil Sketch Using Python
  • picture matching game using python
  • Hangman in Python
  • Tic-Tac-Toe Game Using Python

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Rock Paper Scissors Python
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Automatic image slider in html css How to Make Automatic Image Slider in JavaScript
Next Article Flying Helicopter with html and css How to Make a Flying Helicopter with HTML, CSS
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

python QR code generator

How To Make Python QR Code Generator

November 25, 2022
Countdown Calendar using Python Tkinter

How to Make Countdown Calendar using Python Tkinter

November 24, 2022
binary search algorithm in python

What is Binary Search Algorithm in python?

November 23, 2022

What is Linear search in python?

November 23, 2022
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?