A Rock-Paper-Scissors Game: Python Tutorial for Beginners
Are you learning Python and itching to move beyond basic "Hello World" scripts? You’re ready to build something interactive.
The best way to learn programming is by creating real projects. Today, we are going to build one of the most classic games ever devised: Rock-Paper-Scissors.
This is the perfect beginner Python project. Why? Because despite its simplicity, it forces you to use three absolute cornerstones of programming:
Handling User Input (Asking the player for their choice).
Computer Randomness (Making the computer unpredictable).
Conditional Logic (Using
if,elif, andelseto decide who wins).
By the end of this tutorial, you will have a fully functional, interactive command-line game to show off.
Let's get coding!
The Game Plan
Before we write code, let's understand the logic. A game of Rock-Paper-Scissors involves three steps:
The Player chooses Rock, Paper, or Scissors.
The Computer randomly selects one of the three options.
The Comparison: We compare the two choices based on the classic rules:
Rock smashes Scissors.
Scissors cuts Paper.
Paper covers Rock.
Step-by-Step Python Implementation
We will build this game in distinct chunks so it's easy to understand. Open your favorite code editor (like VS Code, PyCharm, or simple IDLE) and start a new file named rps_game.py.
Step 1: Setting Up and Importing Randomness
Python is powerful, but it doesn't know how to be "random" by default. We need to import a built-in tool called the random module to help the computer make its choice.
We also need to define the three possible moves.
import random
# Define the available choices in a list
choices = ["rock", "paper", "scissors"]
print("Let's play Rock, Paper, Scissors!")
Step 2: Getting the Player's Choice
We need to ask the user to type in their move.
Pro-Tip for Beginners: Users are unpredictable. They might type "Rock", "rock", or "ROCK". To make our program robust, we will convert whatever they type into lowercase letters using Python's .lower() method.
# Get input from the user and convert it to lowercase
player_choice = input("Enter your choice (rock, paper, scissors): ").lower()
# Basic validation to make sure they typed something valid
if player_choice not in choices:
print("Invalid choice! You must choose rock, paper, or scissors.")
# Exit the program if input is invalid (simple way for now)
exit()
Step 3: The Computer's Turn
Now we need the computer to pick one of the three items from our choices list randomly. Python makes this incredibly easy with random.choice().
# The computer picks randomly from the list
computer_choice = random.choice(choices)
print(f"\nYou chose: {player_choice}")
print(f"Computer chose: {computer_choice}")
print("-" * 30) # Just printing a separator line
Step 4: Determining the Winner (The Logic)
This is the brain of our Python program. We need to use conditional statements (if, elif, else) to compare the moves.
The easiest way to approach this is:
Check for a tie first.
Check all the ways the player can win.
If it's not a tie, and the player didn't win, the computer must have won.
Here is the code for the logic:
# Determine the winner
# Check for a tie first
if player_choice == computer_choice:
print("It's a tie!")
# Check for all player win conditions
elif (player_choice == "rock" and computer_choice == "scissors") or \
(player_choice == "paper" and computer_choice == "rock") or \
(player_choice == "scissors" and computer_choice == "paper"):
print("🎉 You win! 🎉")
# If it's not a tie, and you didn't win, you lose.
else:
print("😢 Computer wins! Better luck next time.")
The Complete Code
Here is the full script put together. You can copy this directly into your Python editor to test it out.
import random
def play_rps():
# Define available choices
choices = ["rock", "paper", "scissors"]
print("\n--- ROCK, PAPER, SCISSORS ---")
# --- 1. Get Player Input ---
# We use a loop here to ensure we get valid input
while True:
player_choice = input("Enter rock, paper, or scissors: ").lower()
if player_choice in choices:
break # Input is good, exit loop
print("Invalid input. Please try again.")
# --- 2. Computer Selection ---
computer_choice = random.choice(choices)
print(f"\nYou chose: {player_choice.upper()}")
print(f"Computer chose: {computer_choice.upper()}")
print("-" * 30)
# --- 3. Determine Winner ---
if player_choice == computer_choice:
print("It's a tie!")
elif (player_choice == "rock" and computer_choice == "scissors") or \
(player_choice == "paper" and computer_choice == "rock") or \
(player_choice == "scissors" and computer_choice == "paper"):
print("🎉 You win!")
else:
print("🤖 Computer wins!")
# Run the game
if __name__ == "__main__":
play_rps()
(Note: In the final complete code block above, I added a small while True loop around the input section. This ensures that if the user types gibberish, the program asks them again instead of just quitting. This is a great habit to get into for beginner Python projects!)
Next Steps: Level Up Your Game
Congratulations! You’ve just coded your first interactive Python game.
If you want to challenge yourself further, try adding these features to your code:
Play Again Loop: Wrap the entire game logic in a
whileloop so the user can play multiple rounds without restarting the program.Scorekeeping: Create variables like
player_score = 0andcomputer_score = 0at the top, increment them when someone wins, and display the score at the end of every round.
Happy coding!

Comments
Post a Comment