Practice and reinforce the concepts from Lesson 6
Time needed: 45-60 minutes
What you'll create: An interactive Rock Paper Scissors game where you battle against a computer opponent!
In this activity, you will recreate the classic Rock Paper Scissors game in Python. Players will play against a fair computer robot with only 1 round of game. You'll learn to use lists, random selection, and conditional statements to create an interactive game.
:white_check_mark: Make sure you have completed the lesson on Python Random Module
:white_check_mark: You understand how lists work (storing multiple items)
:white_check_mark: You know how to use if-elif-else blocks
:white_check_mark: Have your EduBlocks account ready to login
:bulb: :bulb: Pro tip: Save your work every 5-10 minutes! Use Ctrl+S (Windows) or Cmd+S (Mac) for quick saves.
Note: To resume a project later, click on the "Projects" icon instead of "Editor".
Steps to Complete Your Rock Paper Scissors Game
Step One: Basic Game Setup (10 minutes)
Let's start building! We'll use index numbers (0, 1, 2) to represent our three choices. This makes it easier for players to pick their move!
- Drag the import block from the Random category
- Find:
import random
- Drop it at the very top of your workspace
- Create your options list
- Drag:
create list
block from Lists category- Name it:
options
- Add three items:
"rock"
,"paper"
,"scissors"
- Display game instructions
- Drag a
- Type:
"Welcome to Rock Paper Scissors!"
- Add another
- Type:
"Enter 0 for rock, 1 for paper, 2 for scissors"
tip :dart: Why use numbers? Using 0, 1, 2 is easier to type than spelling out "rock", "paper", or "scissors". This is how real programmers make things user-friendly!
Time to get the player's choice! We'll convert their number into the actual word (rock, paper, or scissors).
Get player input
input
block"Pick your choice: "
int()
block to convert to numberCreate player variable
create variable
blockplayer
get list item
blockoptions
list:bulb: :warning: Common mistake: Forgetting to convert input to integer! The
input
block gives us text, but list indexes need numbers.
Step 3: Computer Robot Choice (5 minutes)
Let's make the computer pick randomly - this is where the fun begins!
- Create robot variable
- Drag:
create variable
block- Name it:
robot
- Make it random
- From Random category, drag:
random.choice()
- Put your
options
list inside the parentheses- Connect this to your robot variable
Step 4: Display Choices (5 minutes)
Show what each player chose - this builds suspense!
- Print player's choice
- Drag:
- Type:
"You chose: "
- Add another
player
variable- Print robot's choice
- Drag:
- Type:
"Robot chose: "
- Add another
robot
variable
Step 5: Winning Conditions Logic (15 minutes)
Now for the exciting part - determining who wins! Remember the rules:
Rock beats Scissors
Paper beats Rock
Scissors beats Paper
- Start with the if block
- Drag an
if
block from Control category- Check if:
player == robot
- Inside, add
"It's a draw!"
- Add winning conditions using elif
- Click the :emoji:️ gear on your if block
- Add 3
elif
sections- Set up each winning condition:
- First elif:
player == "rock" and robot == "scissors"
- Print:
"You win! Rock crushes scissors!"
- Second elif:
player == "paper" and robot == "rock"
- Print:
"You win! Paper covers rock!"
- Third elif:
player == "scissors" and robot == "paper"
- Print:
"You win! Scissors cut paper!"
- Add the else block
- In the else section, print:
"Robot wins!"
tip :emoji: Logic tip: We only need to check the 3 ways the player can win. If it's not a draw and the player didn't win, then the robot must have won!
Before moving on, let's test what you've built!
:bulb: Tip
:bug: Debugging tip: If your game crashes when you enter 3 or higher, that's normal! We'll fix this in the advanced challenge.
Right now, if someone enters 3, 4, or -1, the program crashes! Let's make it bulletproof.
Wrap your player choice in validation
if
blockif choice >= 0 and choice <= 2
Handle valid input
Handle invalid input
"Your input number is invalid! Please enter 0, 1, or 2"
Real Python Connection: In actual Python programming, this is called "input validation" - checking user input before using it prevents crashes and makes programs more professional!
"My program shows an error when I run it!"
import random
is at the very topint()
around your input"The robot always wins/loses!"
and
between conditions (not or
)"Nothing happens when I enter my choice!"
Before submitting, make sure you have:
:white_check_mark: Random module imported at the top
:white_check_mark: Options list with rock, paper, scissors
:white_check_mark: Player input converted to integer
:white_check_mark: Robot using random.choice()
:white_check_mark: Both choices displayed clearly
:white_check_mark: All winning conditions checked
:white_check_mark: Draw condition handled
:white_check_mark: (Advanced) Input validation for numbers outside 0-2
Great job creating your Rock Paper Scissors game! :tada:
When you're ready to submit:
Note: You need to submit 2 project links:
Remember, making mistakes is part of learning - every programmer has made these same errors before becoming experts!