Rock Paper Scissors Game: A Simple Python Implementation

This is a straightforward implementation of the classic Rock Paper Scissors game. You can play it against another person, or even against a computer using the provided Python code.

The Rules of the Game

  • Rock beats Scissors
  • Scissors beats Paper
  • Paper beats Rock

How to Play

  1. Choose your weapon: Both players (or a player and the computer) choose 'r' (rock), 'p' (paper), or 's' (scissors) to represent their selection.
  2. Compare and declare the winner: The winner is determined based on the rules outlined above.

Code Implementation (Python)

import random

def game():
    print("Welcome to Rock Paper Scissors!")
    player1 = input("Player 1, please choose rock (r), paper (p) or scissors (s): ")
    player2 = input("Player 2, please choose rock (r), paper (p) or scissors (s): ")
    choices = ["r", "p", "s"]
    computer_choice = random.choice(choices)
    print(f"Computer chooses {computer_choice}")
    if player1 == player2:
        print("It's a tie!")
    elif player1 == "r":
        if player2 == "s":
            print("Player 1 wins!")
        else:
            print("Player 2 wins!")
    elif player1 == "p":
        if player2 == "r":
            print("Player 1 wins!")
        else:
            print("Player 2 wins!")
    elif player1 == "s":
        if player2 == "p":
            print("Player 1 wins!")
        else:
            print("Player 2 wins!")
    else:
        print("Invalid input, please try again.")
        game()

game()

Let's break down the Python code:

  1. import random: This line imports the random module, which is essential for generating the computer's choice.
  2. def game():: This line defines a function named game(), which will hold the logic of the game.
  3. print("Welcome to Rock Paper Scissors!"): This line displays a welcoming message to the players.
  4. player1 = input("Player 1, please choose rock (r), paper (p) or scissors (s): "): This line prompts Player 1 to enter their choice.
  5. player2 = input("Player 2, please choose rock (r), paper (p) or scissors (s): "): This line prompts Player 2 to enter their choice.
  6. choices = ["r", "p", "s"]: This line creates a list containing the possible choices (rock, paper, scissors).
  7. computer_choice = random.choice(choices): This line randomly selects a choice for the computer from the choices list.
  8. print(f"Computer chooses {computer_choice}"): This line displays the computer's choice to the players.
  9. if player1 == player2:: This line checks if both players chose the same option. If they did, it's a tie.
  10. elif player1 == "r": and so on: These lines use elif statements to determine the winner based on the rules of the game.
  11. print("Invalid input, please try again."): This line handles invalid user input and prompts the player to try again.
  12. game(): This line recursively calls the game() function if there is invalid input, starting the game again.
  13. game(): This line calls the game() function to start the game when the script is run.

Now you can copy and paste the code into a Python interpreter or a Python file and run it to play the game!

Rock Paper Scissors Game: Python Code & How to Play

原文地址: https://www.cveoy.top/t/topic/lmns 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录