0% found this document useful (0 votes)
576 views

Rock Paper Scissor Game in Python

This document describes how to create a simple Rock-Paper-Scissors game in Python without using external game libraries. The game allows a player to choose Rock, Paper, or Scissors and randomly generates the computer's choice to determine a winner based on the classic rules. It prompts the user for input, makes a random computer selection, compares the choices to determine a winner or draw, and repeats until the user chooses to quit. The document provides code snippets to demonstrate how to get user input, make random selections, compare choices, and loop the game until the user wants to stop playing.

Uploaded by

geetansh ibm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
576 views

Rock Paper Scissor Game in Python

This document describes how to create a simple Rock-Paper-Scissors game in Python without using external game libraries. The game allows a player to choose Rock, Paper, or Scissors and randomly generates the computer's choice to determine a winner based on the classic rules. It prompts the user for input, makes a random computer selection, compares the choices to determine a winner or draw, and repeats until the user chooses to quit. The document provides code snippets to demonstrate how to get user input, make random selections, compare choices, and loop the game until the user wants to stop playing.

Uploaded by

geetansh ibm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Rock Paper Scissor Game in Python

Introduction

Let's create a simple Rock-Paper-Scissor command-line game without using any third-
party game libraries like PyGame. The player will initially have the option to choose either
Rock, Paper, or Scissors in this game. The winner is chosen in accordance with the game's
rules after the computer has made a random selection from the last two alternatives.

Objective
To defeat the opponent by choosing a weapon that defeats their choice according to the
following rules: Rock smashes (or breaks or blunts) scissors (rock wins), scissors cut paper
(scissors win), and paper covers rock (paper wins).

System Requirements

● Python installed

● Any code editor to write/edit the code

Implementation:

In the following implementation, the first rule states that "Rock vs Paper -> Paper Wins."
which means that if a player chooses rock and plays against an opponent who selects paper,
paper will win. The code then proceeds to state that "Rock vs Scissor -> Rock Wins,"
indicating that rock prevails when played against scissors. The last line of code states "Paper
vs Scissor -> Scissor Wins," proving that when playing scissors versus paper, scissors will
win. The program summarizes the winning rules of the Rock, Paper, Scissors game as
follows: "Rock vs Paper -> Paper Wins, Rock vs Scissor -> Rock Wins, Paper vs Scissor ->
Scissor Wins." Moving forward, the code prompts the user to decide and determines the input
value to assign it a corresponding choice: "Rock" if the choice is 1, "paper" if it is 2, or
"scissor" if it is 3. The code then asks the user for their computer turn and randomly generates
a number between 1 and 3 to represent the computer's choice. This process continues until the
computer's choice matches the user's choice. Once the computer selects rock, the code
outputs both options, indicating what happened during the game. The user is prompted to
choose among the options of rock, paper, or scissors, and the code randomly selects one of
these alternatives to represent the computer's turn. The user's selection and the computer's
selection are then printed. Finally, the code cycles back to provide the user with another
option to continue playing.

Input:

# import random module

import random

# Print multiline instruction

# performstring concatenation of string

print("Winning Rules of the Rock paper scissor game as follows: \n"

+ "Rock vs paper->paper wins \n"

+ "Rock vs scissor->Rock wins \n"

+ "paper vs scissor->scissor wins \n")

while True:

print("Enter choice \n 1 for Rock, \n 2 for paper, and \n 3 for scissor \n")

# take the input from user

choice = int(input("User turn: "))

# OR is the short-circuit operator

# if any one of the condition is true

# then it return True value

# looping until user enter invalid input


while choice > 3 or choice < 1:

choice = int(input("enter valid input: "))

# initialize value of choice_name variable

# corresponding to the choice value

if choice == 1:

choice_name = 'Rock'

elif choice == 2:

choice_name = 'paper'

else:

choice_name = 'scissor'

# print user choice

print("user choice is: " + choice_name)

print("\nNow its computer turn.......")

# Computer chooses randomly any number

# among 1 , 2 and 3. Using randint method

# of random module

comp_choice = random.randint(1, 3)
# looping until comp_choice value

# is equal to the choice value

while comp_choice == choice:

comp_choice = random.randint(1, 3)

# initialize value of comp_choice_name

# variable corresponding to the choice value

if comp_choice == 1:

comp_choice_name = 'Rock'

elif comp_choice == 2:

comp_choice_name = 'paper'

else:

comp_choice_name = 'scissor'

print("Computer choice is: " + comp_choice_name)

print(choice_name + " V/s " + comp_choice_name)

# we need to check of a draw

if choice == comp_choice:

print("Draw=> ", end="")


result = "Draw"

# condition for winning

if((choice == 1 and comp_choice == 2) or

(choice == 2 and comp_choice == 1)):

print("paper wins => ", end="")

result = "paper"

elif((choice == 1 and comp_choice == 3) or

(choice == 3 and comp_choice == 1)):

print("Rock wins =>", end="")

result = "Rock"

else:

print("scissor wins =>", end="")

result = "scissor"

# Printing either user or computer wins or draw

if result == Draw:

print("<== Its a tie ==>")

if result == choice_name:
print("<== User wins ==>")

else:

print("<== Computer wins ==>")

print("Do you want to play again? (Y/N)")

ans = input().lower

# if user input n or N then condition is True

if ans == 'n':

break

# after coming out of the while loop

# we print thanks for playing

print("\nThanks for playing")

# Print multiline instruction

# performstring concatenation of string

print("Winning Rules of the Rock paper scissor game as follows: \n"

+ "Rock vs paper->paper wins \n"

+ "Rock vs scissor->Rock wins \n"

+ "paper vs scissor->scissor wins \n")


while True:

print("Enter choice \n 1 for Rock, \n 2 for paper, and \n 3 for scissor \n")

# take the input from user

choice = int(input("User turn: "))

# OR is the short-circuit operator

# if any one of the condition is true

# then it return True value

# looping until user enter invalid input

while choice > 3 or choice < 1:

choice = int(input("enter valid input: "))

# initialize value of choice_name variable

# corresponding to the choice value

if choice == 1:

choice_name = 'Rock'

elif choice == 2:

choice_name = 'paper'

else:

choice_name = 'scissor'
# print user choice

print("user choice is: " + choice_name)

print("\nNow its computer turn.......")

# Computer chooses randomly any number

# among 1 , 2 and 3. Using randint method

# of random module

comp_choice = random.randint(1, 3)

# looping until comp_choice value

# is equal to the choice value

while comp_choice == choice:

comp_choice = random.randint(1, 3)

# initialize value of comp_choice_name

# variable corresponding to the choice value

if comp_choice == 1:

comp_choice_name = 'Rock'

elif comp_choice == 2:
comp_choice_name = 'paper'

else:

comp_choice_name = 'scissor'

print("Computer choice is: " + comp_choice_name)

print(choice_name + " V/s " + comp_choice_name)

# we need to check of a draw

if choice == comp_choice:

print("Draw=> ", end="")

result = Draw

# condition for winning

if((choice == 1 and comp_choice == 2) or

(choice == 2 and comp_choice == 1)):

print("paper wins => ", end="")

result = "paper"

elif((choice == 1 and comp_choice == 3) or

(choice == 3 and comp_choice == 1)):


print("Rock wins =>", end="")

result = "Rock"

else:

print("scissor wins =>", end="")

result = "scissor"

# Printing either user or computer wins or draw

if result == Draw:

print("<== Its a tie ==>")

if result == choice_name:

print("<== User wins ==>")

else:

print("<== Computer wins ==>")

print("Do you want to play again? (Y/N)")

ans = input().lower

# if user input n or N then condition is True

if ans == 'n':

break
# after coming out of the while loop

# we print thanks for playing

print("\nThanks for playing")

Output:

Conclusion
Hence, we have successfully built the Python application that allows us to play the rock,
paper, scissor game in Python which is a classic and nostalgic game and sometimes acts as a
deciding factor in performing any activity in python programming language.

You might also like