How to Create a Rock Paper Scissors Game in Python
Submitted by Yorkiebar on Friday, March 14, 2014 - 07:34.
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...
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...
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...
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...
Finally...
Finally we just output the player and computer choice along with a thank you for playing message...
- import random;
- player = input("Enter your choice (rock/paper/scissors): ");
- player = player.lower();
- while (player != "rock" and player != "paper" and player != "scissors"):
- print(player);
- player = input("That choice is not valid. Enter your choice (rock/paper/scissors): ");
- player = player.lower();
- computerInt = random.randint(0,2);
- if (computerInt == 0):
- computer = "rock";
- elif (computerInt == 1):
- computer = "paper";
- elif (computerInt == 2):
- computer = "scissors";
- else:
- computer = "Huh? Error...";
- if (player == computer):
- print("Draw!");
- elif (player == "rock"):
- if (computer == "paper"):
- print("Computer wins!");
- else:
- print("You win!");
- elif (player == "paper"):
- if (computer == "rock"):
- print("You win!");
- else:
- print("Computer wins!")
- elif (player == "scissors"):
- if (computer == "rock"):
- print("Computer wins!");
- else:
- print("You win!");
- print("Your choice: " + player + "\nComputer choice: " + computer + "\nThank you for playing!");
- input("Enter any key to exit.");
Comments
An Amazing, Brilliant, Fantastic, game! avaliable 2 play for all
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!
One problem I see with this program
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
- Add new comment
- 1917 views