Super Simple Python: Rock Paper Scissors

Super Simple Python is a series dedicated to creating super simple Python projects for beginners to be able to do in under 15 minutes. In this episode, we’ll be covering building a rock paper scissors game in under 30 lines of code!

Video version:

Just like the Super Simple Python: Random Number Generator, we’ll start by importing the random module.

import random

After importing the random module, let’s create a way to generate a random “play”. That is, randomly pick either rock, paper, or scissors. First we’ll make a dictionary that contains three key-value pairs for rock, paper, and scissors. Then we’ll create a generate function that will randomly generate a number and return the corresponding play.

_dict = {
    0: "rock",
    1: "paper",
    2: "scissors"
}
 
def generate():
    num = random.randint(0, 2)
    return _dict[num]

Rock Paper Scissors Game Logic

Next we’ll implement the game logic. We’ll ask the player for rock, paper, or scissors as input and convert that input to lowercase. We convert it to lowercase just to match the casing we wrote in the dictionary. Then we’ll call the generate function to generate the computer’s move.

player = input("Rock, Paper, or Scissors? ").lower()
computer = generate()

Once we have both moves, we’ll just compare them in a series of if and elif statements. Notice that we have to use elif and not else if. That’s just Python specific syntax. We first handle the tie case and then all the other ones.

if player == computer:
    print(f"Tie! You chose {player} and the computer chose {computer}")
elif player == "rock" and computer == "paper":
    print(f"Computer wins! You chose {player} and the computer chose {computer}")
elif player == "rock" and computer == "scissors":
    print(f"You win! You chose {player} and the computer chose {computer}")
elif player == "paper" and computer == "rock":
    print(f"You win! You chose {player} and the computer chose {computer}")
elif player == "paper" and computer == "scissors":
    print(f"Computer wins! You chose {player} and the computer chose {computer}")
elif player == "scissors" and computer == "rock":
    print(f"Computer wins! You chose {player} and the computer chose {computer}")
elif player == "scissors" and computer == "paper":
    print(f"You win! You chose {player} and the computer chose {computer}")

When we run the program to play the game it should look like the images below. Note that we can answer the input prompt with any casing we want and it still works!

rock paper scissors in Python example 1
rock paper scissors in Python result – rock
rock paper scissors in Python example 2
rock paper scissors in Python result – Paper

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

4 thoughts on “Super Simple Python: Rock Paper Scissors

Leave a Reply

Discover more from PythonAlgos

Subscribe now to keep reading and get access to the full archive.

Continue reading