Introduction
The Rock, Paper, Scissors game is a classic hand game played
between two people, where each player simultaneously chooses one
of the three options: rock, paper, or scissors. The rules are simple:
• Rock beats Scissors
• Scissors beats Paper
• Paper beats Rock
In this Python microproject, we simulate the Rock, Paper, Scissors
game where the user plays against the computer. The computer makes
its choice randomly, while the user inputs their choice. The game then
compares the selections and announces the winner. This project is a
great way to practice basic Python concepts such as:
• Conditional statements (if, elif, else)
• Loops
• Functions
• Use of the random module
• Taking input and displaying output
It also introduces beginners to the fundamentals of game logic and
user interaction through the command line. This simple yet engaging
game helps build a strong foundation for more advanced Python
programming projects.
Features of the Python Implementation
1. User Input: The player selects their choice (Rock, Paper, or
Scissors).
2. Computer Choice: The program randomly selects an option.
3. Game Logic: Determines the winner based on the rules.
4. Score Tracking: Keeps track of wins, losses, and ties.
5. Replay Option: Allows the player to continue playing or exit.
Learning Objectives
- Using random module for computer selection.
- Implementing conditional statements (if-elif-else) for game logic.
- Handling user input validation.
- Creating a loop for continuous gameplay.
Source Code:-
import random
def get_computer_choice():
return random.choice(['rock', 'paper', 'scissors'])
def get_user_choice():
choice = input("Enter your choice (rock, paper, scissors): ").lower()
while choice not in ['rock', 'paper', 'scissors']:
choice = input("Invalid choice. Enter rock, paper, or scissors: ").lower()
return choice
def determine_winner(user, computer):
if user == computer:
return "It's a tie!"
elif (
(user == 'rock' and computer == 'scissors') or
(user == 'paper' and computer == 'rock') or
(user == 'scissors' and computer == 'paper')
):
return "You win!"
else:
return "Computer wins!"
def play_game():
print("Welcome to Rock, Paper, Scissors!")
while True:
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"\nYou chose: {user_choice}")
print(f"Computer chose: {computer_choice}")
result = determine_winner(user_choice, computer_choice)
print(result)
print("-" * 30)
# Run the game
play_game()
Output:-