←Previous page

12GAME PROJECTS · ROCK, PAPER, SCISSORS

Play rock, paper, scissors.

You pick, PyBot picks at random, and a function decides who wins. Best of 3!

IN REAL LIFE

A game with fair rules.

Two players choose at the same time. Clear rules say who wins.

Who goes first?

Friends play one quick round to pick who starts a game.

Best of 3

One round can be luck. Three rounds and a score feel fairer.

Video games

Many games have a computer player that picks a move at random.

THE PLAN

The game in four steps.

Each step uses something you already know.

Your choice

Ask the player and check the word is one of the three options.

input + or

PyBot's choice

PyBot picks one word from the list at random.

random.choice

Who wins?

A function compares both choices and returns the winner.

def + if/elif + and

Best of 3

Repeat 3 rounds and add a point to the winner each time.

for + range + counters

RUN IT

Play three rounds.

Guess first. Then run real Python and check.

1

Guess the result

for turn in range(3): you = input("rock, paper or scissors? ") bot = random.choice(options)

PyBot's choices change each run.

How many times does the game ask you to choose?

rps.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change range(3) to range(5) and write 5 answers. Now it is best of 5!

BUILD IT

Build it step by step.

PyBot fixed its choice to rock to test the game. Now every run gives the same result.

0Completed
11Not tried
0Review
01

Step 1: check your choice

paper is a good choice, but PyBot says no. A word can't be all three at once. Fix the if.

Goal output
rock, paper or scissors? paper
Good choice!
OUTPUT

02

Step 2: who wins?

Paper wraps rock, so paper must win. One rule in winner is wrong. Find it and fix it.

Goal output
rock, paper or scissors? paper
PyBot plays rock
Winner: you
OUTPUT

03

Step 3: best of 3

PyBot wrote 3 answers, but the game stops after 2 rounds. Make it play 3.

Goal output
rock, paper or scissors? paper
rock, paper or scissors? scissors
rock, paper or scissors? rock
Score: 1 to 1
OUTPUT

YOUR TURN

Questions about the game.

Try each tiny question. A wrong answer becomes something to review.

04

PyBot's choice

options = ["rock", "paper", "scissors"] bot = random.choice(options)

What can be in the box bot?

05

Same choice

if you == bot: return "tie"

You play scissors. PyBot plays scissors.

What does winner return?

06

Both must be True

elif you == "rock" and bot == "scissors": return "you"

You play rock. PyBot plays paper.

Is this elif True?

07

Not an option

if you == "rock" or you == "paper" or you == "scissors": print("Good choice!") else: print("That is not an option.")

You type: lizard

What will Python show?

08

Who wins this one?

print(winner("scissors", "rock"))

You play scissors. PyBot plays rock.

What will Python show?

09

Keep the score

your_points = 0 your_points += 1 your_points += 1 print(your_points)

What will Python show?

10

Why return?

result = winner(you, bot) if result == "you": your_points += 1

Why does winner return a word instead of printing it?

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Small parts make a full game.

input asks, random.choice picks, a function decides, and for repeats the rounds. Test with a fixed choice first.