← Back to Python series
🛠
Practice Projects · ★
Conditionals · Loops · random

Project 1 — Rock Paper Scissors

Build a best-of-3 Rock Paper Scissors game against the computer with input validation and a running win/loss record.

if/elsewhilerandom
Duration
1–2 hours
Level
📊 Basic Applied
Prerequisite
🎯 Basic Weeks 4–5
OUTCOME
A complete turn-based game with score tracking

What you'll learn

  • 1Validate user input and re-prompt on invalid entry
  • 2Use random.choice for the computer's move
  • 3Separate win/draw/loss logic into a function
  • 4Loop until a player reaches 2 wins

Project Overview

  • Extend the single-round RPS from Basic Week 4 into best-of-3
  • Invalid input triggers a re-prompt (rock/paper/scissors or r/p/s)
  • After each match: 'Play again?' option
  • Track cumulative match record across games

Requirements

  • Input: rock / paper / scissors (or r/p/s shorthand)
  • Computer uses random.choice
  • Show round result and current score after each round
  • 2 wins first = match winner
  • Print overall match record at the end

Sample Session

text
Rock Paper Scissors — Best of 3
[Round 1]  Your move (r/p/s): r
You: rock  |  Computer: scissors  →  You win! (1:0)
[Round 2]  Your move (r/p/s): p
You: paper |  Computer: paper     →  Draw     (1:0)
[Round 3]  Your move (r/p/s): s
You: scissors | Computer: rock    →  You lose (1:1)
... (continues until 2 wins)

Match winner: Computer (2:1)
Play again? (y/n): n

--- Match Record ---
You: 0 wins  Computer: 1 win

Bonus Challenges

  • Add Rock Paper Scissors Lizard Spock rules
  • Have the computer predict player patterns (last 3 moves)
  • Save match record to a JSON file

💻 Examples

Run these examples and check the output yourself.

solution.pyFull implementation
CODE
"""Rock Paper Scissors — Best of 3"""
import random

CHOICES = ["rock", "paper", "scissors"]
SHORT   = {"r": "rock", "p": "paper", "s": "scissors"}
BEATS   = {"rock": "scissors", "paper": "rock", "scissors": "paper"}

def get_move():
    while True:
        raw = input("  Your move (r/p/s): ").strip().lower()
        if raw in CHOICES: return raw
        if raw in SHORT:   return SHORT[raw]
        print("  Please enter rock, paper, or scissors (or r/p/s).")

def judge(player, computer):
    if player == computer: return "draw"
    if BEATS[player] == computer: return "win"
    return "lose"

def play_match():
    p_score = c_score = 0
    rnd = 0
    while p_score < 2 and c_score < 2:
        rnd += 1
        print(f"\n[Round {rnd}]  Score: You {p_score} - Computer {c_score}")
        player   = get_move()
        computer = random.choice(CHOICES)
        result   = judge(player, computer)
        print(f"  You: {player:<10} | Computer: {computer:<10}", end="")
        if result == "win":  p_score += 1; print("  → You win!")
        elif result == "lose": c_score += 1; print("  → You lose!")
        else: print("  → Draw")
    winner = "You" if p_score > c_score else "Computer"
    print(f"\nMatch winner: {winner} ({p_score}:{c_score})")
    return p_score > c_score

def main():
    my_wins = com_wins = 0
    while True:
        if play_match(): my_wins += 1
        else: com_wins += 1
        again = input("\nPlay again? (y/n): ").strip().lower()
        if again != "y": break
    print(f"\n--- Match Record ---\nYou: {my_wins}  Computer: {com_wins}")

if __name__ == "__main__":
    main()

📝 Exercises

Try them yourself first, then open the solution to compare.

Exercise 1

Implement Rock Paper Scissors

Goal: Build the full best-of-3 game described above.

Requirements
  • Input validation with re-prompt
  • Best of 3 (first to 2 wins)
  • Match record tracking
  • Play-again option
Grading
  • · Input validation — 20%
  • · Correct win logic — 40%
  • · Score tracking — 20%
  • · Play-again loop — 20%
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub ↗