๐ŸŽฎ Project: Rock, Paper, Scissors Game in Python

โœ… Concepts Used:


๐Ÿง  Idea:

User plays Rock, Paper, Scissors against the computer. Game repeats until user quits or a score target is hit. Tracks score, validates input, and gives feedback.


๐Ÿ“ฆ Step-by-step code with comments

# ๐Ÿ“Œ Import random module to let computer randomly choose an option
import random

# ๐ŸŽฏ Define valid choices in a list
choices = ["rock", "paper", "scissors"]

# ๐Ÿง  Score trackers
user_score = 0
comp_score = 0

# ๐Ÿ“Œ Function to decide the winner of a round
def decide_winner(user, comp):
    """
    Takes user and computer choice, returns outcome string:
    'win', 'lose', or 'draw'
    """
    if user == comp:
        return "draw"

    # Define winning conditions
    if (user == "rock" and comp == "scissors") or \\
       (user == "paper" and comp == "rock") or \\
       (user == "scissors" and comp == "paper"):
        return "win"

    return "lose"

# ๐Ÿš€ Main game loop
while True:
    print("\\n--- Rock, Paper, Scissors Game ---")
    print("Enter your choice (rock/paper/scissors) or 'q' to quit:")
    user_choice = input("๐Ÿ‘‰ Your move: ").lower().strip()

    # ๐Ÿ›‘ Check for quit
    if user_choice == "q":
        print("๐Ÿ‘‹ Thanks for playing!")
        break

    # โŒ Invalid input handling
    if user_choice not in choices:
        print("โš ๏ธ Invalid input. Please enter rock, paper or scissors.")
        continue

    # ๐Ÿ’ป Computer makes a random choice
    comp_choice = random.choice(choices)
    print(f"๐Ÿค– Computer chose: {comp_choice}")

    # ๐Ÿ” Decide winner and update scores
    result = decide_winner(user_choice, comp_choice)

    if result == "win":
        print("โœ… You win this round!")
        user_score += 1
    elif result == "lose":
        print("โŒ You lost this round.")
        comp_score += 1
    else:
        print("๐Ÿค It's a draw!")

    # ๐Ÿงพ Display current score
    print(f"๐Ÿ“Š Score -> You: {user_score} | Computer: {comp_score}")

๐Ÿ› ๏ธ Suggestions for Improvement (Try These if You Have Time):

Feature Concept Used Level
Track history of moves Lists Easy
Add round number Loops & Counters Easy
Play up to N rounds Loop + Break Conditions Medium
Create a score file (score.txt) File Handling Medium
Add emojis using Unicode Fun UX Easy
GUI version (Tkinter) Python Module Medium
Function to replay game Function Composition Medium
Use main() structure Good Coding Practice Medium
Use *args or **kwargs to handle optional input Chapter 8 Medium

๐Ÿ” Example Output: