In: Computer Science
The following is the python script for the rock, scissors, and paper game. Making use of nested if-else block will do for this game. Proper explanation is given in comments.
#import randint from random module
from random import randint
#This is the list of shapes during play
shapes=["rock","paper","scissors"]
#Here we assign a random from the shapes to computer
computer=shapes[randint(0,2)]
#Initially set player to False
flag=True
#make a count of all rounds, which include tie,player_wins,computer_wins
count=0
tie=0
player_wins=0
computer_wins=0
while flag: #This loop continues till player enters -1
player=input("Rock, Paper, Scissors? ").lower()
count+=1 #take count increment by 1
if player==computer: #if player and computer are equal, then its a tie
print("It's a Tie!!!")
tie+=1 #increment tie by 1
elif player=="rock":
if computer=="paper":
print("You lose!! -> "+computer+" wins")
computer_wins+=1 #increment computer_wins by 1
else:
print("You win!! -> "+player+" wins")
player_wins+=1 #increment player_wins by 1
#same rules respectively for other shapes
elif player=="paper":
if computer=="scissors":
print("You lose!! -> "+computer+" wins")
computer_wins+=1
else:
print("You win!! -> "+player+" wins")
player_wins+=1
elif player=="scissors":
if computer=="rock":
print("You lose!! -> "+computer+" wins")
computer_wins+=1
else:
print("You win!! -> "+player+" wins")
player_wins+=1
elif player=="-1": #if player enters -1, then stop the game
count-=1 #donot count this attempt
flag=False #set flag to False
else: #else, this is an invalid option
count-=1 #donot count this attempt
print("Invalid shape!! Please try again")
computer=shapes[randint(0,2)] #assign computer some random shape
#print the game result
print("Game Over!!!")
print("Number of games played "+str(count))
print("Number of player wins "+str(player_wins))
print("Number of computer wins "+str(computer_wins))
print("Number of Tie "+str(tie))
I am also attaching the output and code screenshot for your reference. It will help you to correct if any indentation errors.
Output and code screenshot:
#Please don't forget to upvote if you find the solution
helpful. Feel free to ask doubts if any, in the comments section.
Thank you.