ROCK – PAPER – SCISSORS
You may have played rock paper scissors before. Maybe you’ve used it
to decide who pays for dinner or who gets first choice of players for a
team.
If you’re unfamiliar, rock paper scissors is a hand game for two or more
players. Participants say “rock, paper, scissors” and then simultaneously
form their hands into the shape of a rock (a fist), a piece of paper (palm
facing downward), or a pair of scissors (two fingers extended). The rules
are straightforward:
Rock smashes scissors.
Paper covers rock.
Scissors cut paper.
Now that you have the rules down, you can start thinking about how
they might translate to Python code.
Play a Single Game of
Rock Paper Scissors in Python
Using the description and rules above, you can make a game
of rock paper scissors. Before you dive in, you’re going to need
to import the random module you’ll use to simulate the
computer’s choices:
import random
Take User Input
Taking input from user is pretty straightforward in Python. The goal here
is to ask the user what they would like to choose as an action and then
assign that choice to a variable:
user_action = input("Enter a choice (rock, paper, scissors): ")
Make the Computer Choose
A competitive game of rock paper scissors involves strategy. Rather than
trying to develop a model for that, though, you can save yourself some
time by having the computer select a random action.
possible_actions = ["rock", "paper", "scissors"]
computer_action = [Link](possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
Determine a Winner
Now that both players have made their choice, you just need a
way to decide who wins. Using an if … elif … else block, you
can compare players’ choices and determine a winner
if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")
By comparing the tie condition first, you get rid of quite a few
cases. If you didn’t do that, then you’d need to check each
possible action for user_action and compare it against each
possible action for computer_action. By checking the tie
condition first, you’re able to know what the computer chose
with only two conditional checks of computer_action.
And that’s it! All combined, your code should now look like
this:
import random
user_action = input("Enter a choice (rock, paper, scissors): ")
possible_actions = ["rock", "paper", "scissors"]
computer_action = [Link](possible_actions)
print(f"\nYou chose {user_action}, computer chose {computer_action}.\n")
if user_action == computer_action:
print(f"Both players selected {user_action}. It's a tie!")
elif user_action == "rock":
if computer_action == "scissors":
print("Rock smashes scissors! You win!")
else:
print("Paper covers rock! You lose.")
elif user_action == "paper":
if computer_action == "rock":
print("Paper covers rock! You win!")
else:
print("Scissors cuts paper! You lose.")
elif user_action == "scissors":
if computer_action == "paper":
print("Scissors cuts paper! You win!")
else:
print("Rock smashes scissors! You lose.")
You’ve now written code to take in user input, select a
random action for the computer, and decide the winner! But
this only lets you play one game before the program finishes
running.
Project by:------
Devesh Uniyal
XI – ‘D’
Acknowledgement
I take this opportunity to express my profound gratitude and
deep regards to my guide and mentor, Mrs. Jaspreet Kaur Bahl,
for her exemplary guidance, monitoring and constant
encouragement throughout the course of this project, her
blessing, help and guidance given by her time to time shall
carry me a long way in the journey of life on which I am about
to embark.
I also take this opportunity to express a deep sense of
gratitude to Mr. M.S. Rawat Chairman of Mayur Public School
for the cordial support, valuable information and guidance
which helped me in completing this task through various
stages.
I am obliged to my subject teachers of my school for the
valuable information which was being provided by them in
their respective field. I am grateful for their cooperation during
the period of my assignment.
INDEX
INDEX
CERTIFICATE
ACKNOWLEDGEMENT
INTRODUCTION
GIVING OUR INPUT
TAKING THE COMPUTER’S
DECISION
DETRMINING A WINNER
COMPLETE CODE
CERTIFICATE
This is to certify that this computer science project entitled
'ROCK-PAPER-SCISSORS’
Submitted to `MAYUR PUBLIC SCHOOL, IP EXTENSION ' is
a bonafide record of work done by ‘Siddhant Mishra’ under
my supervision and guidance which is being submitted. I
wish them all success in life.
[Link] Kaur Bahl
PGT Computer science
MAYUR PUBLIC SCHOOL IP Extension, Delhi-1010092