TalentJR

๐Ÿ Python for kids ยท Class 6 ยท Lesson 18 of 18 ยท Free

Capstone: Rock Paper Scissors Game

โฑ About 45 minutes ยท โ–ถ run real Python here โ€” nothing to install ยท no sign-in

In this lesson you will

  • Write a function that decides the winner using complex conditions or a dictionary.
  • Check the player's input with a loop and repeat rounds until a match is won.
  • Plan a bigger program by splitting it into small, tested functions.

Stone, paper, scissors โ€“ you have played it to decide who bats first! Now you will teach a computer to play it. This capstone uses nested if, complex conditions, loop patterns, functions and dictionaries, all in one game.

How do I plan a rock paper scissors game in Python?

Rules: rock breaks scissors, scissors cut paper, and paper covers rock. Same choices give a tie.

Before coding, list the jobs. Get the player's move. Get the computer's move. Decide the winner. Keep score.

Each job will become a small function. We test each one before joining them together.

๐Ÿ Python โ€” you can edit this code
Output
Choices: ['rock', 'paper', 'scissors']
Number of choices: 3
Possible match-ups: 9

๐Ÿ’ก There are 9 match-ups: 3 ties, 3 player wins and 3 computer wins.

How do I decide the winner with if and or?

First check for a tie. Then list the three ways the player can win, joined with or.

Brackets around each pair keep the and parts together. If none match, the computer must have won.

The function returns a word instead of printing. That lets the rest of the program use the result.

๐ŸŽฌ How Python thinks โ€” step 1 of 13Python is about to run line 1
โ–ถdef winner(player, computer):
2 if player == computer:
3 return "tie"
4 if (player == "rock" and computer == "scissors") or (player == "scissors" and computer == "paper") or (player == "paper" and computer == "rock"):
5 return "player"
6 return "computer"
7
8print(winner("rock", "scissors"))
9print(winner("paper", "scissors"))
10print(winner("rock", "rock"))
๐Ÿ“ฆ Variables (memory boxes)

No variables yet.

๐Ÿ–ฅ๏ธ Output
โ–Œ
โœ๏ธ Change this code and run it yourself
๐Ÿ Python โ€” you can edit this code
Output
player
computer
tie

Can a dictionary make the winner rules shorter?

A dictionary can store what each move beats. beats["rock"] is "scissors", and so on.

Now the win check is one short line: does the player's move beat the computer's move?

If you add new moves later, you only change the dictionary. The function stays the same.

๐ŸŽฌ How Python thinks โ€” step 1 of 10Python is about to run line 1
โ–ถbeats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
2
3def winner(player, computer):
4 if player == computer:
5 return "tie"
6 elif beats[player] == computer:
7 return "player"
8 return "computer"
9
10print(winner("scissors", "paper"), winner("rock", "paper"))
๐Ÿ“ฆ Variables (memory boxes)

No variables yet.

๐Ÿ–ฅ๏ธ Output
โ–Œ
โœ๏ธ Change this code and run it yourself
๐Ÿ Python โ€” you can edit this code
Output
player computer

How do I make sure the player types a valid move?

Players make typing mistakes, like Rock with a capital R or a spelling error like sissors.

lower() and strip() clean the text first. Then a while True loop asks again until the move is in the list.

This is the retry pattern from the loop patterns lesson, now inside a function.

๐ŸŽฌ How Python thinks โ€” step 1 of 12Python is about to run line 1
โ–ถdef get_move():
2 while True:
3 move = input("rock, paper or scissors? ").lower().strip()
4 if move in ["rock", "paper", "scissors"]:
5 return move
6 print("Please type one of the three moves.")
7
8choice = get_move()
9print("You chose", choice)
๐Ÿ“ฆ Variables (memory boxes)

No variables yet.

๐Ÿ–ฅ๏ธ Output
โ–Œ
โœ๏ธ Change this code and run it yourself
๐Ÿ Python โ€” you can edit this code
Output
rock, paper or scissors? sissors
Please type one of the three moves.
rock, paper or scissors?  Rock 
You chose rock

๐Ÿ’ก return inside the loop ends the function at once, so no break is needed.

How does the computer choose its move?

random.choice() picks one item from a list. That makes the computer hard to predict.

For testing, a fixed list of computer moves is better. We use that in the full game below, so the result is the same every run.

When your game works, swap the fixed list for random.choice() to play for real.

๐Ÿ Python โ€” you can edit this code
Output
Computer plays scissors
Computer plays rock
Computer plays rock

๐Ÿ’ก The output changes every run, because random.choice() picks a new move each time.

How do I put the full best-of-3 game together?

Now we join the functions. A while loop keeps playing rounds until someone has 2 wins.

A dictionary keeps the score for player and computer. Ties do not count towards anyone.

Read the main loop slowly. Every line uses something from this course.

๐Ÿ Python โ€” you can edit this code
Output
Your move: paper
Computer: rock -> player
Your move: rock
Computer: paper -> computer
Your move: papr
Invalid move, try again.
Your move: paper
Computer: paper -> tie
Your move: rock
Computer: scissors -> player
Final score: {'player': 2, 'computer': 1}
You are the champion!

๐Ÿ’ก "player" if ... else "computer" is a one-line if. It picks the first value when the check is True.

๐Ÿค” Predict the output

Think like Python: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
print(beats["paper"])
print(beats[beats["rock"]])

Question 2. What does this print?

p = "scissors"
c = "rock"
if p == c:
    print("tie")
elif (p == "rock" and c == "scissors") or (p == "scissors" and c == "paper"):
    print("win")
else:
    print("lose")

Question 3. What does this print?

score = {"me": 0, "cpu": 0}
for r in ["me", "tie", "cpu", "me"]:
    if r != "tie":
        score[r] = score[r] + 1
print(score["me"], score["cpu"])

๐Ÿงฉ Your challenge

Add a "first to 3 wins" match with a winning streak message. Use the fixed computer moves given. After each round, print the round result. If the player wins 2 rounds in a row, print "On fire!". At the end, print the final score.

Expected output (with the inputs given):
Your move: paper
Round 1: you paper, computer rock -> player
Your move: paper
Round 2: you paper, computer rock -> player
On fire!
Your move: rock
Round 3: you rock, computer paper -> computer
Your move: rock
Round 4: you rock, computer scissors -> player
Final score: {'player': 3, 'computer': 1}
๐Ÿ Python โ€” you can edit this code

โš ๏ธ Common mistakes (and how to fix them)

โŒ Wrong

p = "scissors"
c = "rock"
if p == "rock" and c == "scissors" or p == "paper" and c == "rock" or p == "scissors":
    print("You win")
else:
    print("You lose")

No error message โ€“ but choosing scissors always wins, even against rock.

โœ… Right

p = "scissors"
c = "rock"
if (p == "rock" and c == "scissors") or (p == "paper" and c == "rock") or (p == "scissors" and c == "paper"):
    print("You win")
else:
    print("You lose")

The last part forgot to check the computer's move. Write every pair in full, and use brackets to keep pairs together.

โŒ Wrong

beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
move = input("Your move: ")
print("You beat", beats[move])

KeyError: 'Rock'

โœ… Right

beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
move = input("Your move: ").lower()
print("You beat", beats[move])

Dictionary keys must match exactly. "Rock" with a capital R is not the key "rock", so use lower().

โŒ Wrong

score = 0
def add_point():
    score = score + 1

add_point()
print(score)

UnboundLocalError: cannot access local variable 'score' where it is not associated with a value

โœ… Right

def add_point(score):
    return score + 1

score = 0
score = add_point(score)
print(score)

Inside the function, score is a new local variable with no value yet. Pass the score in and return the new value.

๐Ÿ“Œ Remember

  • Plan first: list the jobs, then write one small function per job.
  • A tie check plus three bracketed and-pairs joined with or decides the winner.
  • A dictionary of what each move beats makes the rules short and easy to change.
  • lower(), strip() and a retry loop keep the player's input valid.
  • Test with fixed computer moves, then switch to random.choice() to play for real.
  • A while loop with a score dictionary runs a best-of-3 match.

Finished the quiz and the challenge?

Questions students ask

How does random.choice work in Python?

random.choice(my_list) returns one item from the list, picked by chance. You need import random first.

Why does my game crash with a KeyError?

The typed move is not a key in your dictionary. Clean the input with lower() and check it with in first.

How can I add Rock Paper Scissors Lizard Spock?

Let each move beat two others. Store a list in the dictionary, like "rock": ["scissors", "lizard"], and check with in.

Why test with fixed moves instead of random ones?

Fixed moves give the same result every run. You can check your score logic is right before adding chance.