
Python Rock Paper Scissors Game
How to Create a Rock, Paper, Scissors Game in Python
Rock, Paper, Scissors is a classic hand game played between two people. Let’s create a simple command line version of the game using Python.
The Game Logic
In the game, each player simultaneously forms one of three shapes with their hand. The possible shapes are rock, paper, and scissors. The winner is determined by the rules:
- Rock crushes scissors
- Scissors cuts paper
- Paper covers rock
Getting Started
To create the game, we will use Python and follow these steps:
- Take user input using
input() - Implement a loop to play several games
- Utilize
Enumobjects and functions to clean up the code - Define the game rules using a dictionary
Let’s dive into the code!
The Python Code
First, let’s import the necessary module:
from enum import EnumNext, let’s define the game options as an enumeration:
class Choice(Enum):
ROCK = 1
PAPER = 2
SCISSORS = 3Now, let’s write the game logic to determine the winner:
def determine_winner(player_choice, computer_choice):
if player_choice == computer_choice:
return "It's a tie!"
if (player_choice == Choice.ROCK and computer_choice == Choice.SCISSORS) or (player_choice == Choice.SCISSORS and computer_choice == Choice.PAPER) or (player_choice == Choice.PAPER and computer_choice == Choice.ROCK):
return "You win!"
else:
return "Computer wins!"Finally, let’s implement the game loop and user input:
def main():
while True:
player_input = input("Enter your choice (rock, paper, scissors) or 'quit' to exit: ").upper()
if player_input == 'QUIT':
break
try:
player_choice = Choice[player_input]
computer_choice = # generate computer's choice using random module
print(f"Your choice: {player_choice.name}")
print(f"Computer's choice: {computer_choice.name}")
print(determine_winner(player_choice, computer_choice))
except KeyError:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()Conclusion
You’ve now created a simple Rock, Paper, Scissors game using Python. This project serves as a great introduction to game programming and demonstrates the use of input, loops, Enum objects, functions, and dictionaries in Python.
Feel free to further enhance the game by adding features like keeping score or creating a graphical user interface. Happy coding!




