Skip to main content

How to Create a Rock Paper Scissors Game in Python


Introduction:
This tutorial will be covering how to make a simple rock, paper, scissors game in Python.

How It Works:
First we get the user choice through input.
Then we generate a random number 1 through 3.
Convert the random number to rock, paper or scissors.
Output the comparison result.

Imports:
To generate a random computer choice we need to import the random module...

  1. import random;

User Choice:
Next we want to get the user input, convert it to lower case (easier to check) then validate their choice. If their choice is not valid, they must re-enter until it is valid...

  1. player = input("Enter your choice (rock/paper/scissors): ");
  2. player = player.lower();
  3. while (player != "rock" and player != "paper" and player != "scissors"):
  4. print(player);
  5. player = input("That choice is not valid. Enter your choice (rock/paper/scissors): ");
  6. player = player.lower();

Computer Choice:
As mentioned earlier, the computer choice will be random using the random module. So first we generate a random number out of 0, 1 or 2 then we convert that in to a string choice...

  1. computerInt = random.randint(0,2);
  2. if (computerInt == 0):
  3. computer = "rock";
  4. elif (computerInt == 1):
  5. computer = "paper";
  6. elif (computerInt == 2):
  7. computer = "scissors";
  8. else:
  9. computer = "Huh? Error...";

The Winner:
Finally we must decide on the winner. First we check if the computer and player choices are the same, if they are then we output that it's a draw. Otherwise we check the rest of the possibilities...

  1. if (player == computer):
  2. print("Draw!");
  3. elif (player == "rock"):
  4. if (computer == "paper"):
  5. print("Computer wins!");
  6. else:
  7. print("You win!");
  8. elif (player == "paper"):
  9. if (computer == "rock"):
  10. print("You win!");
  11. else:
  12. print("Computer wins!")
  13. elif (player == "scissors"):
  14. if (computer == "rock"):
  15. print("Computer wins!");
  16. else:
  17. print("You win!");

Finally...
Finally we just output the player and computer choice along with a thank you for playing message...

  1. print("Your choice: " + player + "\nComputer choice: " + computer + "\nThank you for playing!");
  2. input("Enter any key to exit.");

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted bysandeep maharaj (not verified)on Mon, 11/23/2015 - 07:36

This is dank :)

Submitted byweweweew (not verified)on Sat, 12/31/2016 - 03:36

how do we add a while loop on this code so if the user loses they continue until the user wins

Submitted byjoel campbell (not verified)on Fri, 09/29/2017 - 02:52

In reply to by weweweew (not verified)

I would guess a simple:
for counter in range(10):
and then insert an indentation here followed by the code

Submitted bymike pogson (not verified)on Tue, 02/28/2017 - 22:23

This game is very intriguing to all people at ages between 5 all the way to the age of 70! (70 because I really like this game!)

one main problem of this game is that the things which are typed above are not English. and it does not use proper grammar. for example, it says "elif" this does not make sense!

Submitted byBob Mallarkey (not verified)on Sun, 04/30/2017 - 02:35

I have tried entering this code into Python IDLE, but it does not seem to be taking my input. Can anyone help me? Thank you, and btw, great job on your code, Yorkiebar! ;)

Submitted byruchifromskl (not verified)on Tue, 11/28/2017 - 23:07

wow so good man

Submitted byX (not verified)on Fri, 12/01/2017 - 00:49

On line three of user choice it is trying to test if player = all three its a simple issue that all you have to do is replace the ands with ors.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.