Python
Python

 

Introduction

Rock, Paper, Scissors is a classic hand game often played between two people. The players count to three and simultaneously form one of three shapes with their hand: Rock (a fist), Paper (an open hand), or Scissors (a fist with the index and middle fingers extended). The game follows a simple set of rules:

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

The goal of this program is to simulate a single-player game where the user plays against the computer. The objective is to create a simple Python-based Rock, Paper, Scissors game where the user will select their choice, and the computer will randomly choose one. The program will then determine the winner based on the rules above.

Objective

The objective of the program is to allow the user to play a game of Rock, Paper, Scissors against the computer. The program will:

  • Allow the user to input their choice (Rock, Paper, or Scissors).
  • Generate a random choice for the computer.
  • Determine the winner based on the rules.
  • Display the results: whether the player won, lost, or if it was a draw.

Python Code

import random

def get_computer_choice():
    choices = ['rock', 'paper', 'scissors']
    return random.choice(choices)

def determine_winner(player_choice, computer_choice):
    if player_choice == computer_choice:
        return "It's a tie!"
    elif (player_choice == 'rock' and computer_choice == 'scissors') or \
         (player_choice == 'scissors' and computer_choice == 'paper') or \
         (player_choice == 'paper' and computer_choice == 'rock'):
        return "You win!"
    else:
        return "You lose!"

def play_game():
    print("Welcome to Rock, Paper, Scissors!")
    
    while True:
        player_choice = input("Enter your choice (rock, paper, or scissors): ").lower()
        
        if player_choice not in ['rock', 'paper', 'scissors']:
            print("Invalid input, please choose 'rock', 'paper', or 'scissors'.")
            continue
        
        computer_choice = get_computer_choice()
        print(f"Computer chooses: {computer_choice}")
        
        result = determine_winner(player_choice, computer_choice)
        print(result)
        
        play_again = input("Do you want to play again? (yes/no): ").lower()
        if play_again != 'yes':
            print("Thanks for playing!")
            break

if __name__ == "__main__":
    play_game()

Program Structure

Let’s break down the code:

  • get_computer_choice(): This function generates a random choice for the computer from the list [‘rock’, ‘paper’, ‘scissors’] using the random.choice() method.
  • determine_winner(player_choice, computer_choice): This function takes the player’s and computer’s choices as input and checks the rules to determine the winner. It returns the result as a string.
  • play_game(): This is the main game loop that runs until the player decides to stop. The player is prompted to make a choice, the computer’s choice is generated, and the winner is determined and displayed. The loop continues until the player types ‘no’ when asked if they want to play again.
  • if __name__ == “__main__”: This line ensures that the game starts only if this script is run directly (not imported into another Python file).

How to Run the Program

To run this Rock, Paper, Scissors game on your computer, follow these steps:

  1. Make sure you have Python installed on your system (you can download it from python.org).
  2. Copy the code into a text editor, and save the file with a .py extension, for example, rock_paper_scissors.py.
  3. Open a terminal (Command Prompt on Windows or Terminal on macOS/Linux).
  4. Navigate to the folder where your Python file is saved using the cd command.
  5. Type python rock_paper_scissors.py and hit Enter to start the game.

The program will run in the terminal, and you will be prompted to make your choice. Enjoy playing!

© 2024 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)