Rock Paper Scissor with Python

Aakash Verma
3 min readSep 28, 2019

Hi there ! , I’m up with another article for you to get acquainted with the beginner level Rock, Paper ,Scissor game with the code implementation in python…this is basically for my personal edification and enhancing my skills follow through to get the taste of this ….

Here is the code !

Random and time are the two modules that needs to be imported while implementing the code .In total there are six functions that are part of the game code and performs the desired functionality . So, Let’s look through the code…

#Rock Paper Scissor in Pythonimport random
import time
rock = 1
paper = 2
scissor = 3
names = {rock : "Rock", paper : "Paper", scissor : "Scissor"}
#Rules that Define the Player's Winning Scenario
condition = {rock : scissor, paper : rock, scissor : rock}
#Initializes player's score and computer's score to 0
computer_score = 0
player_score = 0

Basically it contains a start() function that starts the game by prompting user to make choice of either Rock, Paper or Scissor .

# start the game
def start():
print("Let's start with the Game of Rock, Paper and Scissor")
while game():
pass
scores()

we have game () function which make use of random module to generate a pseudorandom number for computer’s choice out of 1, 2, and 3 .And finally computer’s choice and player’s choice is passes to result function to get compared and display the result .

def game():
"""This method calls for player's move and passes the player's choice to result function to compare both"""
player = move()
#Makes a random move for computer out [1, 2, 3]
computer = random.randint(1, 3)
#check for result player and computers move
result(player, computer)
return (play_again())

move() function that prompts player for making it’s move . It let’s user enter any one out of 1, 2, and 3 , anything entered other than this is rejected and user is prompted to renter the value .

def move():
"""This function handles player choice of either Rock, Paper or Scissor"""
while True:
print()
player = input("Rock = 1\n Paper = 2\nScissor = 3\nMake A Move : ")
try :
#check if player entered a valid input
player = int(player)
if player in [1,2,3]:
return player
#if invalid input raise Error
except ValueError:
pass
print("Oops! I didn't understand that. please enter 1, 2 or 3.")

After getting user’s choice we pass it to result() function which compares the choice of computer and player and give decision out of it . If player won’ s , player is greeted with win message and if lost then greeted with defeated message . there is also a provision for Tie Game , which is the case in which player and computer makes same choice . In this case player is prompted with Tie game message.

def result(player, computer):
"""Evaluates result out of Player's Choice and Computer's Choice"""
for i in range(1,4):
print("."*i,)
#makes execution stop for 1 sec
time.sleep(1)
print("",end="")
# print Computer's random choice out of [1, 2, 3]
print("Computer throw {0}!".format(names[computer]))
global player_score, computer_score
#if choice of Player and Computer are equal print Tie Game
if player == computer:
print("Tie Game")
else:
#check if Player's Choice an Computer's Choice make a win for Player
if condition[player] == computer:
print("Aha !, You have won")
player_score += 1
#if not return Player lost
else:
print("You are no better than ME . You Lost")
computer_score += 1

Fifth function is play_again() that prompts user asking for it’s choice to play again .It also calls calls scores() function which displays Player’s and Computer’s Total Scores .

def play_again():
"""Prompts User To Play again"""
scores()
answer = input("Would You Like To play Again (y/n):")
if answer in ["y", "Y"]:
return answer
else:
#if player says no then greet him and exit()
print("Thank You very much for playing our Game .See you next Time!.")

def scores():
"""prints Scores of both computer and player"""
global player_score, computer_score
print("HIGH SCORES")
print("Player :",player_score)
print("Computer : ",computer_score)
if __name__ == "__main__":
start()

So above was one of the beginner friendly code project and it was fully practical implementation as I like it .This was one of the things that I did at my early days of Python Learning and if you are a beginner I highly recommend it to you too . I hope You find it helpful and do comment your thought about it .

--

--

Aakash Verma

Computer Science Undergrad ,Tech Enthusiast , Loves writing about Technology