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

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

4 thoughts on “Super Simple Python: Rock Paper Scissors

Leave a Reply

%d bloggers like this: