In this article, we will discuss how to create a rock paper, and scissors game in python.
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.
- Import the random module
- Initialize the global variables for choices as a tuple of r, p, s, and computer score and player score as 0.
- Create a function for getting the player’s choice.
- Inside the function get the input from the user.
- 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.
- Create a function to return the computer’s choice as input from the random. choice function.
- Create a game function inside which we will check who scores the mount for the current game.
- Call the player’s choice function and save the answer in a variable.
- Call the computer choice function and save it in another variable.
- Now, check if both the answers are the same, if so then print its a tie and no one gets a point.
- Following are the scenarios where the player gets a point,
- Player choice- “r” computer choice- “s”
- Player choice- “s” computer choice- “p”
- Player choice- “p” computer choice- “r”
- In all the other conditions computer scores a point.
- Now, print both the points to the user.
- Now, in our main part of the program, we need to write a welcome message.
- Call the game function 4 times for running the game for 4 rounds.
- Ask the user if the user wants to continue/ reset and continue/quit.
- If the user wants to continue repeat step 7.
- If the user wants to reset and continue re-declare the variables for player score and computer score to zero and repeat step 7
- If the player wants to quit you can print thank you for playing and exit the game.
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!")
- 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.