This code implements a game of **Rock, Paper, Scissors** that...

February 4, 2025 at 04:17 AM

import random, sys print('ROCK, PAPER, SCISSORS') # These variables keep track of the number of wins, losses, and ties. wins = 0 losses = 0 ties = 0 while True: # The main game loop. print('%s Wins, %s Losses, %s Ties' % (wins, losses, ties)) while True: # The player input loop. print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit') playerMove = input() if playerMove == 'q': sys.exit() # Quit the program. if playerMove == 'r' or playerMove == 'p' or playerMove == 's': break # Break out of the player input loop. print('Type one of r, p, s, or q.') # Display what the player chose: if playerMove == 'r': print('ROCK versus...') elif playerMove == 'p': print('PAPER versus...') elif playerMove == 's': print('SCISSORS versus...') # Display what the computer chose: randomNumber = random.randint(1, 3) if randomNumber == 1: computerMove = 'r' print('ROCK') elif randomNumber == 2: computerMove = 'p' print('PAPER') elif randomNumber == 3: computerMove = 's' print('SCISSORS') # Display and record the win/loss/tie: if playerMove == computerMove: print('It is a tie!') ties = ties + 1 elif playerMove == 'r' and computerMove == 's': print('You win!') wins = wins + 1 elif playerMove == 'p' and computerMove == 'r': print('You win!') wins = wins + 1 elif playerMove == 's' and computerMove == 'p': print('You win!') wins = wins + 1 elif playerMove == 'r' and computerMove == 'p': print('You lose!') losses = losses + 1 elif playerMove == 'p' and computerMove == 's': print('You lose!') losses = losses + 1 elif playerMove == 's' and computerMove == 'r': print('You lose!') losses = losses + 1 Let’s look at this code line by line, starting at the top. import random, sys print('ROCK, PAPER, SCISSORS') # These variables keep track of the number of wins, losses, and ties. wins = 0 losses = 0 ties = 0 First, we import the random and sys module so that our program can call the random.randint() and sys.exit() functions. We also set up three variables to keep track of how many wins, losses, and ties the player has had. while True: # The main game loop. print('%s Wins, %s Losses, %s Ties' % (wins, losses, ties)) while True: # The player input loop. print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit') playerMove = input() if playerMove == 'q': sys.exit() # Quit the program. if playerMove == 'r' or playerMove == 'p' or playerMove == 's': break # Break out of the player input loop. print('Type one of r, p, s, or q.')

This code implements a game of Rock, Paper, Scissors that the player plays against the computer. Here's what the program does step-by-step:

  1. Imports modules:

    • random: Used to make random choices for the computer's moves.
    • sys: Used to allow the program to exit when the player chooses to quit.
  2. Displays the game title:

    • Prints "ROCK, PAPER, SCISSORS" at the start to introduce the game.
  3. Initializes win/loss/tie counters:

    • Uses variables wins, losses, and ties to track how many games have been won, lost, or tied by the player.
  4. Starts the main game loop (while True):

    • Continuously runs the game until the player decides to quit.
  5. Displays the current game stats:

    • Prints the total wins, losses, and ties after each round.
  6. Prompts the user for input within a second loop:

    • Asks the player to select their move ((r)ock, (p)aper, (s)cissors) or quit the game with (q)uit.
    • If the player enters q, the program exits immediately using sys.exit().
    • If the player inputs an invalid option, it re-prompts them until they input a valid move (either 'r', 'p', 's', or 'q').
  7. Handles the player’s input:

    • Displays a message about the player's selected move (e.g., "ROCK versus...").
  8. Randomly generates the computer’s move:

    • Using random.randint(1, 3), a random number between 1 and 3 is chosen:
      • 1 for rock, 2 for paper, 3 for scissors.
    • Displays a message about the computer's selected move.
  9. Compares the player’s move with the computer’s move:

    • Determines whether the round results in a win, loss, or tie:
      • A tie occurs when the player’s and computer’s moves are the same.
      • The player wins when their move beats the computer's move:
        • Rock (r) beats scissors (s).
        • Paper (p) beats rock (r).
        • Scissors (s) beats paper (p).
      • If the player doesn’t win or tie, they lose the round.
    • Updates the appropriate counter (wins, losses, or ties) based on the result.
  10. Loops the game:

    • After determining the outcome of the current round, it goes back to the start of the main game loop to ask for the player's next move and play another round.

Main functionalities:

  • Players can quit the game at any time by entering q.
  • The game keeps track of statistics (wins, losses, and ties) across the rounds.
  • The computer’s move is chosen randomly, ensuring an unpredictable and fair opponent.

In short, this code implements an interactive and replayable Rock, Paper, Scissors game.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node