RPS

Rock Paper Scissor Game in python

In this article, we will discuss how to create a rock paper, and scissors game in python.

Outline

We will ask the user to select either rock paper or scissors. And we will also make the computer choose one out of three randomly. Now we will check who won the game and give them a score. This is done 5 times to say who has won finally and we will ask the user if they want to continue or reset and continue or exit the game.

Algorithm

  1. Import the random module
  2. Initialize the global variables for choices as a tuple of r, p, s, and computer score and player score as 0.
  3. Create a function for getting the player’s choice.
    1. Inside the function get the input from the user.
    2. Check if the input is valid, If yes then return the answer to the function call. If no, then return the function call so that it will again ask for input from the user.
  4. Create a function to return the computer’s choice as input from the random. choice function.
  5. Create a game function inside which we will check who scores the mount for the current game.
    1. Call the player’s choice function and save the answer in a variable.
    2. Call the computer choice function and save it in another variable.
    3. Now, check if both the answers are the same, if so then print its a tie and no one gets a point.
    4. Following are the scenarios where the player gets a point,
      1. Player choice- “r” computer choice- “s”
      2. Player choice- “s” computer choice- “p”
      3. Player choice- “p” computer choice- “r”
    5. In all the other conditions computer scores a point.
    6. Now, print both the points to the user.
  6. Now, in our main part of the program, we need to write a welcome message.
  7. Call the game function 4 times for running the game for 4 rounds.
  8. Ask the user if the user wants to continue/ reset and continue/quit.
    1. If the user wants to continue repeat step 7.
    2. If the user wants to reset and continue re-declare the variables for player score and computer score to zero and repeat step 7
    3. If the player wants to quit you can print thank you for playing and exit the game.
See also  Input Function and Basic Operators in Python

Program with explanation in comments

import random #importing the random module

#initializing the variables
choices = ('r', 'p', 's')
cPoints = 0
pPoints = 0

def player():
  
  '''This function is used to get players choice.'''
  
  global choices #declaring the variable choices to be global
  symbol = input("Choose either rock(r), paper(p), or scissors(s). ").lower()
  
  
  if symbol not in choices:
    print("You did not enter a valid option. ")
    return player() #using recursion to get a valid input
  else:
    return symbol#if valid input return the answer

def computer():

  '''This function is used to get computers choice'''\
  
  return random.choice(choices)

def game():

  '''This function is used to play a single round of game'''
  
  global cPoints, pPoints #declaring cPoints and pPoints to be global
  pChoice = player()#getting the players choice
  cChoice = computer()#getting the computers choice
  print("The computer has chosen", cChoice)
  print("You chose", pChoice)
  if pChoice == cChoice: #checking for tie
    print("Its a tie! No one gets a point. ")
  elif (pChoice == "r" and cChoice == "s") or (pChoice == "s" and cChoice == "p") or (pChoice == "p" and cChoice == "r"): #checking if player scores a point
    
    print("You won! ")
    pPoints += 1 #increment the players point by 1
  else: #checking if computer scores a point
    print("Aww. I won. ")
    cPoints += 1 #increment the computer point by 1
  print()

print("Welcome to Rock Paper and Scissors game!!!")
while True: #creating an infinite loop
  for i in range(5):#loop that runs 5 times
    game() #calling the game for every single round
  print("Good job!\nYour score is:", pPoints, "\nMy score is", cPoints) #displaying the result after 5 rounds
  print()
  again = int(input("Press 1 to continue\nPress2 to reset and continue\npress 3 to exit "))
  if again==1: #this is executed if the user wants to play another 5 rounds
    continue
  elif again==2: #this is executed if the user wants to reset the scores and play another 5 rounds
    pPoints=0
    cPoints=0
    continue
  else: #this is executed if the user wants to quit.
    break
    
print("Ok! Bye!")

Please note:

  • The lower() method converts the input given by the user to lowercase. 
  • Global variables are used to increase the scope of the variable throughout the function. To know more about global variables, Global Variables
  • Recursion is the process of calling the same function inside the function definition. To know about recursion, recursion.
See also  Space Shooter Game Using Python - python project

A detailed explanation of the topics covered

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.