How to Create a Rock Paper Scissors Game in Python

Submitted by: 
Language: 
Visitors have accessed this post 54401 times.

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.");

Comments

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.

wow so good man

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! ;)

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!

elif is a variation of else but elif does not take a condition

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

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

This is dank :)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.