Rock Paper Scissors
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:
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
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
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:------
Siddhant Mishra
XI – ‘D’
28
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
WHAT IS STONE PAPER
SCISSORS?
TAKING THE USER INPUT
MAKING THE COMPUTER
DECIDE
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.