How to make a Rock Paper Scissors game in Python:

December 8, 2022 Off By Zak Morris

Welcome to this tutorial on how to make a Rock Paper Scissors game in Python! This game will challenge the user to play a game of Rock Paper Scissors against the computer.

First, let’s import the necessary modules. We will be using the random module to generate a random move for the computer player and the sys module to exit the program if the user decides to quit.

Python
import random
import sys


Next, we need to create a function that will simulate the computer’s move in the game. We will do this using the randint() method from the random module. This method will generate a random integer between 1 and 3, which we will use to determine the computer’s move (1 for rock, 2 for paper, and 3 for scissors).

Python
def computer_move():
move = random.randint(1, 3)
if move == 1:
return "rock"
elif move == 2:
return "paper"
else:
return "scissors"


Now, let’s create a function that will determine the winner of the game. This function will take in the user’s move and the computer’s move as arguments and will return the winner of the game. If the user and the computer make the same move, the game will be a tie.

Python
def determine_winner(user_move, computer_move):
if user_move == computer_move:
return "Tie"
elif (user_move == "rock" and computer_move == "scissors") or (user_move == "paper" and computer_move == "rock") or (user_move == "scissors" and computer_move == "paper"):
return "User"
else:
return "Computer"


Now, let’s create a while loop that will continue until the user decides to quit the game. Inside the while loop, we will prompt the user to enter their move and convert it to lowercase to ensure it matches the format of the computer’s move. We will then call the computer_move() and determine_winner() functions to generate the computer’s move and determine the winner of the game, respectively. We will print the result of the game to the user and prompt them to either continue playing or quit the game.

Python
while True:
user_move = input("Enter your move (rock, paper, scissors, or q to quit): ").lower()
if user_move == "q":
sys.exit()
computer_move = computer_move()
winner = determine_winner(user_move, computer_move)
print("The computer played {}. The winner is {}".format(computer_move, winner))


And that’s it! We have created a Rock Paper Scissors game in Python that will challenge the user to play a game against the computer. The user can enter their move and the computer will generate a random move, and the winner of the game will be determined and printed to the user. The user can continue playing or quit the game at any time.

Here is the complete code:

Python
import random
import sys

def computer_move():
    move = random.randint(1, 3)
    if move == 1:
        return "rock"
    elif move == 2:
        return "paper"
    else:
        return "scissors"

def determine_winner(user_move, computer_move):
    if user_move == computer_move:
        return "Tie"
    elif (user_move == "rock" and computer_move == "scissors") or (user_move == "paper" and computer_move == "rock") or (user_move == "scissors" and computer_move == "paper"):
        return "User"
    else:
        return "Computer"

while True:
    user_move = input("Enter your move (rock, paper, scissors, or q to quit): ").lower()
    if user_move == "q":
        sys.exit()
    comp_move = computer_move()
    winner = determine_winner(user_move, comp_move)
    print("The computer played {}. The winner is {}".format(comp_move, winner))