๐ŸŽ‰ Welcome to PyVerse! Start Learning Today

Rock Paper Scissors Game

Basic
Python

Build a classic game with Python

1) Project Overview

You will build a simple game you can play in the console: Rock, Paper, Scissors. You choose rock, paper, or scissors. The computer randomly chooses too. The program compares both choices, tells you who wins the round, and keeps score. You can play many rounds until you decide to stop.

2) Learning Objectives

By the end, you will be able to:

  • Use input() and print() to talk to the user.
  • Store and update data in variables.
  • Use if, elif, else to make decisions.
  • Use loops (while) to repeat actions and validate input.
  • Use the random module to make the computer choose.
  • Write and call simple functions to organize code.
  • Work with strings (lowercasing, trimming spaces).

3) Step-by-Step Explanation

  1. Start a new Python file named rps.py.
  2. Import the random module. This lets the computer make a random choice.
  3. Make a list of valid choices: rock, paper, scissors.
  4. Write a helper function normalize_choice(text) that:
    • Cleans up the user's input (lowercase, remove spaces).
    • Accepts r/rock, p/paper, s/scissors.
    • Returns the full word or None if the input is not valid.
  5. Write a function get_computer_choice() that picks a random choice from the list.
  6. Write a function decide_winner(player, computer) that:
    • Returns "tie" if both are the same.
    • Returns "player" if the player's choice beats the computer's.
    • Otherwise returns "computer".
  7. In main():
    • Print a welcome message and explain the rules.
    • Set player_score, computer_score, and ties to 0.
    • Loop: ask the player for a choice, validate it, get the computer's choice, show both, decide the winner, update the scores, and print the score.
    • Ask if the player wants to play again (y/n). If n, end the loop and show the final result.
  8. Run your program and test it with different inputs.

4) Complete and well-commented Python code

Copy and paste the code below into a file named rps.py, then run it.

# Rock Paper Scissors - Beginner Friendly Version import random # Lets the computer make random choices # A list of valid full-word choices CHOICES = ["rock", "paper", "scissors"] def normalize_choice(user_text): """ Turn the user's input into one of: 'rock', 'paper', or 'scissors'. Accepts short forms: r, p, s. Returns None if invalid. """ if not user_text: return None s = user_text.strip().lower() # remove spaces and lowercase if s in ("r", "rock"): return "rock" elif s in ("p", "paper"): return "paper" # Accept both 'scissor' and 'scissors' just in case elif s in ("s", "scissor", "scissors"): return "scissors" else: return None def get_computer_choice(): """ Randomly pick rock, paper, or scissors for the computer. """ return random.choice(CHOICES) def decide_winner(player, computer): """ Compare the player's choice and the computer's choice. Returns 'player', 'computer', or 'tie'. Rules: - Rock beats Scissors - Paper beats Rock - Scissors beat Paper """ if player == computer: return "tie" if player == "rock" and computer == "scissors": return "player" elif player == "paper" and computer == "rock": return "player" elif player == "scissors" and computer == "paper": return "player" else: return "computer" def main(): # Welcome and rules print("Welcome to Rock, Paper, Scissors!") print("Rules: Rock beats Scissors, Paper beats Rock, Scissors beat Paper.") print("Type r, p, s or the full words. Let's play!\n") # Scores player_score = 0 computer_score = 0 ties = 0 while True: # Ask for the player's choice and validate it user_input = input("Choose Rock (r), Paper (p), or Scissors (s): ") player_choice = normalize_choice(user_input) # If invalid, keep asking until it's valid while player_choice is None: print("Oops! That's not a valid choice.") user_input = input("Please type r, p, s or rock, paper, scissors: ") player_choice = normalize_choice(user_input) # Computer picks computer_choice = get_computer_choice() # Show choices print(f"You chose: {player_choice}") print(f"Computer chose: {computer_choice}") # Decide winner result = decide_winner(player_choice, computer_choice) # Update and display scores if result == "player": player_score += 1 print("You win this round! ๐ŸŽ‰") elif result == "computer": computer_score += 1 print("Computer wins this round! ๐Ÿค–") else: ties += 1 print("It's a tie. ๐Ÿ˜") print(f"Score => You: {player_score} | Computer: {computer_score} | Ties: {ties}\n") # Ask if the player wants to play again again = input("Play again? (y/n): ").strip().lower() while again not in ("y", "n", "yes", "no"): again = input("Please type y or n: ").strip().lower() if again in ("n", "no"): break print() # blank line before the next round # Final results print("\nThanks for playing!") print(f"Final Score => You: {player_score} | Computer: {computer_score} | Ties: {ties}") if player_score > computer_score: print("Overall winner: YOU! Great job! ๐Ÿ†") elif computer_score > player_score: print("Overall winner: Computer. Good game! ๐Ÿค") else: print("Overall: It's a tie! Well played! โš–๏ธ") # This makes sure main() runs when you run the file directly. if __name__ == "__main__": main()

5) Output examples

Example run 1:

Welcome to Rock, Paper, Scissors!
Rules: Rock beats Scissors, Paper beats Rock, Scissors beat Paper.
Type r, p, s or the full words. Let's play!

Choose Rock (r), Paper (p), or Scissors (s): stone
Oops! That's not a valid choice.
Please type r, p, s or rock, paper, scissors: r
You chose: rock
Computer chose: rock
It's a tie. ๐Ÿ˜
Score => You: 0 | Computer: 0 | Ties: 1

Play again? (y/n): y

Choose Rock (r), Paper (p), or Scissors (s): paper
You chose: paper
Computer chose: rock
You win this round! ๐ŸŽ‰
Score => You: 1 | Computer: 0 | Ties: 1

Play again? (y/n): n

Thanks for playing!
Final Score => You: 1 | Computer: 0 | Ties: 1
Overall winner: YOU! Great job! ๐Ÿ†

Example run 2:

Choose Rock (r), Paper (p), or Scissors (s): s
You chose: scissors
Computer chose: rock
Computer wins this round! ๐Ÿค–
Score => You: 0 | Computer: 1 | Ties: 0

Play again? (y/n): n

Thanks for playing!
Final Score => You: 0 | Computer: 1 | Ties: 0
Overall winner: Computer. Good game! ๐Ÿค

6) Small Exercise (Challenge)

Add a "First to 3 points" mode

  • Before the game starts, ask the player to choose normal mode or "first to 3."
  • In "first to 3," keep playing rounds until either the player or the computer reaches 3 points, then automatically end the game and announce the champion.

Hints:

  • Use another while loop condition like while player_score < 3 and computer_score < 3.
  • Print how many points are left to win after each round.

7) Summary

Awesome work! You built a complete game that uses input, random choices, conditions, and loops. These are the same building blocks used in bigger projects. Keep experimentingโ€”small changes can lead to fun new features. You've got this!