Your First Python Game: Rock, Paper, Scissors

Your First Python Game: Rock, Paper, Scissors

I’m guessing you’re learning Python because you want to write interactive programs, like video games. Well, today is your lucky day. You’re going to program your first Python game.

Your First Python Game: Rock, Paper, Scissors

Open IDLE and create a new file, Then enter the following:

Experienced programmers will be very quick to point out that there are better ways to write a “Rock, Paper, Scissors” Python game. But, for a beginner, it’s very important to be able to understand and follow the program. Let’s break it down…

First, we import randint  from the random module. This is how our computer opponent will play.

Then we create a list of play options:

There are three possible plays you and the computer can make on each turn, “Rock”, “Paper” and “Scissors”.

Next we setup our players, the computer and you:

We assign a random play to the computer using our list, t, and the randint function. Why (0,2)? Remember that computers start counting at 0. So “Rock” is in the 0 position, “Paper” is in the 1, and so on. Unlike playing RPS with friends in meatspace, the computer has made its play and is waiting for you to take your turn. Also unlike playing RPS with friends in meatspace, the computer isn’t go to cheat and change its play after you make yours. We set you, the player, to False. Why? I’m glad you asked. Let’s take a look at the body of our program the while loop:

Once the while loop starts, the computer will patiently wait for you to make a play. As soon as you take your turn, your status changes from False to True because any value assigned to the variable player makes player True. We use the input() function to pass the new value to the variable player. Your input will determine which statement is triggered below.

Using nested if/elif/else statements, we check every possible outcome of the game and return a message stating the winner, a tie, or an error.

We use else at the end to catch anything that isn’t “Rock”, “Paper” or “Scissors”. Finally we reset the player value to False to restart the while loop.

Your first Python game. Easy, right? Next up, for loops.

Your First Python Game: Rock, Paper, Scissors was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion
  • T Abbott

    This is my first python game, so please forgive what is probably a stupid question. The game seems to only allow you to play it once. Is it possible to play repeatedly without re-running the module? My guess is yes, since we go to the trouble of resetting the player to False. Thanks for the help!

  • Thanks for getting in touch! There was a formatting error in the code which is now corrected above. You need to set the Player to False inside the while loop to keep the game running. You’ll also need to press Ctrl + C to exit.

  • T Abbott

    Great. Thank you. That makes sense. I think you will also want to set the computer’s random answer within the while loop as well. Otherwise, the computer’s answer always remains the same.

  • Thanks for catching that.

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid