Python Series track

Project 1 · Practice Projects

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.

Practice Projects1 min readFree lesson

Difficulty: Lv ★ | Prerequisites: Basic Weeks 4-5 (Conditionals, Loops, random)

Goal

User vs Computer Rock-Paper-Scissors — best of 3, with a cumulative overall record

Differences from Basic Assignment

  • Basic Week 4 = outputs the result once and ends
  • Here = best of 3 + infinite loop + re-request for wrong input + cumulative overall record

Requirements

  • Input: Rock, Paper, Scissors (full word or r/p/s)
  • The computer uses random.choice
  • Display the result every round (win/loss/draw, current score)
  • The first to reach 2 wins takes the match
  • After a match, ask "Play one more game?"
  • Accumulate the overall match record (wins/losses)

Input/Output Example

[Round 1]  Current Score (0 : 0)
Choose (Rock/Paper/Scissors or r/p/s): Scissors
Computer: Paper → User Wins!

[Round 2]  Current Score (1 : 0)
Choose (Rock/Paper/Scissors or r/p/s): Rock
Computer: Rock → Draw

...

🏆 Match Winner: User (2:1)

One more game? (y/n): n

[Overall Record]  User wins: 1, Computer wins: 0

Thanks for playing!

Challenge Task

  • The computer learns user patterns (predict the next move from the last 3 inputs)
  • Save the overall record to a file

Execution

python solution.py

Grading Checklist

  • Re-request wrong input
  • Best of 3 handled correctly
  • Accumulate the overall match record
  • Greet on exit

Assignment

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

solution.pyPython
"""Rock-Paper-Scissors — best of 3, first to 2 wins, with an overall match record"""
import random

CHOICES = ["Rock", "Paper", "Scissors"]
SHORT = {"r": "Rock", "p": "Paper", "s": "Scissors"}
WIN = {"Rock": "Scissors", "Paper": "Rock", "Scissors": "Paper"}


def get_user_choice():
    while True:
        raw = input("Choose (Rock/Paper/Scissors or r/p/s): ").strip()
        choice = raw.capitalize()
        if choice in CHOICES:
            return choice
        if raw.lower() in SHORT:
            return SHORT[raw.lower()]
        print("⚠️ Please enter Rock, Paper or Scissors")


def play_round(round_num, user_score, com_score):
    print(f"\n[Round {round_num}]  Current Score ({user_score} : {com_score})")
    user = get_user_choice()
    com = random.choice(CHOICES)
    print(f"Computer: {com}", end=" ")
    if user == com:
        print("→ Draw")
        return user_score, com_score
    if WIN[user] == com:
        print("→ User Wins!")
        return user_score + 1, com_score
    print("→ Computer Wins!")
    return user_score, com_score + 1


def play_match():
    us = cs = 0
    round_num = 1
    while us < 2 and cs < 2:
        us, cs = play_round(round_num, us, cs)
        round_num += 1
    winner = "User" if us >= 2 else "Computer"
    print(f"\n🏆 Match Winner: {winner} ({us}:{cs})")
    return winner


def main():
    user_wins = com_wins = 0
    while True:
        winner = play_match()
        if winner == "User":
            user_wins += 1
        else:
            com_wins += 1
        if input("\nOne more game? (y/n): ").strip().lower() != "y":
            break
    print(f"\n[Overall Record]  User wins: {user_wins}, Computer wins: {com_wins}")
    print("Thanks for playing!")


if __name__ == "__main__":
    main()

Learn this lesson with a mentor

Work through the Python Series track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now