Rock Paper Scissors

Rock Paper Scissors in Python: Your First Python Project

Learn to create Rock Paper Scissors game using Python. This program is a project based approach for teaching python in a fun way. It intends you to teach basic concepts like working of loops, setting varables and more.

If you want to learn Python with projects you are in the right place. In this article,  we’ll learn how we can create a rock, paper, scissor game using Python.

Rock Paper Scissor is one of the most traditional games that you can easily build in Python. It helps you to develop many basic concepts that are important for kick-starting your Python journey. 

Rock, paper scissor game is similar to a well-known kids game in which players choose a random number of fingers and based on these numbers they decide whether to choose rock paper or season.

How the Rock Paper Scissors game works?

Since this game is computer versus user, so we have user move and computer move.

In Computer move, Computer chooses a random number from a list of three numbers 1, 2, 3 and based on these numbers, the computer chooses one of the Rock, paper or scissor

Priority of choices

  • Paper has higher priority as compared to Rock.
  • Scissor has higher priority than paper but least priority then rock.
  • In the same way, rock has a higher priority than scissors but less priority than paper.

User movie, user have a priority to select Rock paper or Scissors. 

  • If user choice matches with computer one there is a tie the game.
  • One who has a priority of choice than the other one wins the game.

Python Implementation of Rock, Paper, Scissors game

Import random and sys, as we require randomly draw one of –  rock paper scissor and also want to exit the program on demand of the user. Then we are initializing three variables with win, loss and tie and all set to 0. 

  
import random, sys

print("\n \t\t\t\t ****** Rock Paper Scissors *****")

# Trck win, loss and tie.
win = 0
loss = 0
tie = 0

Loop into the main game and set the exit condition when the user presses q exit() will be called to exit the game. The randit function chooses a  random number from 1 2 and 3 based on these three random numbers we are setting computer move as Rock paper and scissor, with if and elif statements. 

 

  
# Loop the main game.
while True:
    print(f"Win: {win}\nLoss: {loss}\nTie: {tie}")

    print("""Enter Your Move: 
            r - rock 
            p - paper 
            s - scissors 
            q - quit""")
    UserMove = input("\n Type one of r, p, s or q : ")
    if UserMove == 'q':
        sys.exit()   #Quit the program.

    # Display what the computer choice: 
    randomNumber = random.randint(1, 3)
With if and else conditionals we are setting the computer move as per our numerical choices we have.

#Let Computer Choose it's move. 
    if randomNumber == 1:
        computerMove = 'r'
        print("Rock")
    
    elif randomNumber == 2:
        computerMove = 'p'
        print("Paper")
    
    elif randomNumber == 3:
        computerMove = 's'
        print("Scissors")
 
We are now setting a situation if there is a tie, it means that if the user and computer moves are the same we are incrementing the tied variable with 1 and no loss or win in the game. Winning logic is set according to the priority mentioned above. Between rock and scissor, rock has higher priority and so incrementing the win with one. Similarly, all this priority are set according to priority rules. If there is a win, the win variable is incremented by 1 and if there is a loose,  loose variable is incremented by 1.
 
#Check Win
    if UserMove == computerMove:
        print("It is tie:!")
        tie += 1
    elif UserMove == 'r' and computerMove == 's':
        print("You win!")
        win += 1
    elif UserMove == "p" and computerMove == 'r':
        win += 1
        print("You win!")
    elif UserMove == "s" and computerMove == 'p':
        win += 1
        print("You win!")
    elif UserMove == "r" and computerMove == 'p':
        loss += 1
        print("You Lose!")
    elif UserMove == "p" and computerMove == 's':
        loss += 1
        print("You Lose!")
    elif UserMove == "s" and computerMove == 'r':
        loss += 1
        print("You Lose!")
    

Final Code for Rock Paper and Scisssors

 
import random, sys

print("\n \t\t\t\t ****** Rock Paper Scissors *****")

# Trck win, loss and tie.
win = 0
loss = 0
tie = 0

# Loop the main game.
while True:
    print(f"Win: {win}\nLoss: {loss}\nTie: {tie}")

    print("""Enter Your Move: 
            r - rock 
            p - paper 
            s - scissors 
            q - quit""")
    UserMove = input("\n Type one of r, p, s or q : ")
    if UserMove == 'q':
        sys.exit()   #Quit the program.

    # Display what the computer choice: 
    randomNumber = random.randint(1, 3)

    #Let Computer Choose it's move. 
    if randomNumber == 1:
        computerMove = 'r'
        print("Rock")
    
    elif randomNumber == 2:
        computerMove = 'p'
        print("Paper")
    
    elif randomNumber == 3:
        computerMove = 's'
        print("Scissors")
        
    #Check Win
    if UserMove == computerMove:
        print("It is tie:!")
        tie += 1
    elif UserMove == 'r' and computerMove == 's':
        print("You win!")
        win += 1
    elif UserMove == "p" and computerMove == 'r':
        win += 1
        print("You win!")
    elif UserMove == "s" and computerMove == 'p':
        win += 1
        print("You win!")
    elif UserMove == "r" and computerMove == 'p':
        loss += 1
        print("You Lose!")
    elif UserMove == "p" and computerMove == 's':
        loss += 1
        print("You Lose!")
    elif UserMove == "s" and computerMove == 'r':
        loss += 1
        print("You Lose!")

OutPut

Final Thoughts

This was the implementation of Rock Paper and Scissors game.  Let me know if you have any question in the comment below.

The Yuvas will be providing you all necessary information related to learning code, exam and other Government and skill development courses.

Pin It on Pinterest