Create a rock, paper, scissors game
In this exercise, you build a rock, paper, scissors game in Python. The player types their choice, the computer picks one at random, and your program uses conditional logic to decide who wins. You practice using comparison operators, if/elif/else statements, and logical operators (and, or, not).
This exercise takes approximately 30 minutes.
Set up your workspace
You’ll write and run your code in Visual Studio Code.
-
Open Visual Studio Code.
-
Select File > Open Folder, create a new folder called
rock-paper-scissorssomewhere on your machine, and open it. -
In the Explorer panel, select the New File icon and name the file
game.py. -
Make sure your Python environment is active. You should see a Python version displayed in the bottom status bar. If you see a warning, select it and choose your installed Python interpreter.
Set up your starter code
Before writing any logic, you’ll paste a set of guiding comments into game.py. Because part of this program uses an if/else block, the starter code includes that structure with comment placeholders inside each branch — so you always know exactly where to add each piece of code.
-
Copy the following code and paste it into
game.py:import random # Display the game title # Get and display the player's choice # Check if the player's choice is invalid # Display an invalid input message # Play the game if the player's choice is valid # Generate the computer's choice # Determine the winnerNote: Be sure to maintain the correct indentation levels.
Display the game title
-
Beneath the
# Display the game titlecomment, add the following lines:print("Rock, Paper, Scissors!") print("-" * 23) -
Select the ▶ Run Python File button in the top-right corner, or open the terminal (Terminal > New Terminal) and run:
python game.py -
Verify the output:
Rock, Paper, Scissors! -----------------------
Get the player’s choice
-
Beneath the
# Get and display the player's choicecomment, add the following lines:player = input("Enter your choice (rock, paper, scissors): ").lower() print(f"You chose: {player}")The
.lower()method converts whatever the player types to lowercase, soRock,ROCK, androckare all treated the same way. -
Run the program, type a choice when prompted, and verify the output:
Rock, Paper, Scissors! ----------------------- Enter your choice (rock, paper, scissors): Rock You chose: rock
Validate the player’s input
Now you’ll replace the placeholder if condition with the real validation check. You’ll use comparison operators and the and logical operator to confirm the player entered a valid choice.
-
Beneath the
# Check if the player's choice is invalidcomment, add the following line:if player != "rock" and player != "paper" and player != "scissors":The
andoperator ensures the error message only shows if the input matches none of the valid options. -
Beneath the
# Display an invalid input messagecomment, add:print("Invalid choice. Please enter rock, paper, or scissors.")Be sure to maintain the correct indentation levels beneath the
ifstatement. -
Run the program and test it with an invalid entry such as
lizard:Enter your choice (rock, paper, scissors): lizard You chose: lizard Invalid choice. Please enter rock, paper, or scissors. -
Run it again with a valid choice to confirm no error appears.
Generate the computer’s choice
The computer needs to pick at random. Python includes a built-in random module with a choice() function that selects a random item from a list — that import random line at the top of your file loads it.
-
First, add an
elseblock beneath the comment# Play the game if the player's choice is valid.else:This ensures the computer only picks a choice if the player entered a valid one.
-
Beneath the
# Generate the computer's choicecomment (inside theelseblock), add the following lines:computer = random.choice(["rock", "paper", "scissors"]) print(f"Computer chose: {computer}")Be sure to maintain the correct indentation levels beneath the
elsestatement. -
Run the program a few times with a valid choice and observe that the computer’s choice changes each time.
Determine the winner
Now you’ll add the game logic inside the else block. You’ll use if/elif/else with the == comparison operator and and to check every winning condition.
-
Beneath the
# Determine the winnercomment (inside theelseblock), add the following lines:if player == computer: print("It's a tie!") elif player == "rock" and computer == "scissors": print("You win! Rock crushes scissors.") elif player == "paper" and computer == "rock": print("You win! Paper covers rock.") elif player == "scissors" and computer == "paper": print("You win! Scissors cuts paper.") else: print(f"Computer wins! {computer.capitalize()} beats {player}.")Each
elifusesandto check two conditions at once: both the player’s choice and the computer’s choice must match for that branch to run. -
Your complete
game.pyshould now look like this:import random # Display the game title print("Rock, Paper, Scissors!") print("-" * 23) # Get and display the player's choice player = input("Enter your choice (rock, paper, scissors): ").lower() print(f"You chose: {player}") # Check if the player's choice is invalid if player != "rock" and player != "paper" and player != "scissors": # Display an invalid input message print("Invalid choice. Please enter rock, paper, or scissors.") # Play the game if the player's choice is valid else: # Generate the computer's choice computer = random.choice(["rock", "paper", "scissors"]) print(f"Computer chose: {computer}") # Determine the winner if player == computer: print("It's a tie!") elif player == "rock" and computer == "scissors": print("You win! Rock crushes scissors.") elif player == "paper" and computer == "rock": print("You win! Paper covers rock.") elif player == "scissors" and computer == "paper": print("You win! Scissors cuts paper.") else: print(f"Computer wins! {computer.capitalize()} beats {player}.") -
Run the program several times to test each possible outcome — win, lose, and tie.
Rock, Paper, Scissors! ----------------------- Enter your choice (rock, paper, scissors): rock You chose: rock Computer chose: scissors You win! Rock crushes scissors.
Extend with GitHub Copilot
Now that the game is working, use GitHub Copilot to extend it. Open the Copilot Chat panel in VS Code (Ctrl+Alt+I) and try the following prompts.
Explore other ways to write conditions
“In Python, what is the ‘in’ operator, and how could I use it to simplify checking if a player’s input is valid?”
Python has a cleaner alternative to chaining != with and. Ask the AI to explain it and try rewriting your input validation line.
Understand short-circuit evaluation
“What is short-circuit evaluation in Python’s ‘and’ and ‘or’ operators? Can you show me an example?”
This is an important behavior to understand when your conditions involve function calls or expressions that could raise errors.
Think about edge cases
“What are some edge cases I should test in a rock, paper, scissors game? How could conditional logic handle them?”
Use the AI’s answer to think critically about inputs your current program doesn’t handle — then decide which ones are worth adding.