In: Computer Science
Subjects: Write a Python program that allows users to play the popular rock-paper-scissor game against the computer multiple times. This program assesses your knowledge of decision structures, repetition structures, and functions.
Requirements:
1.Define a function named as play_game. This function receives two parameters representing the player’s and computer’s choices, and it returns an integer representing the game result from the player’s perspective. Specifically, it returns 1 if the player wins and 0 if the player does not win. Hint: you can reuse most of the code of team activity 2 as the body of this function.
2.Define the main function. In the main function, write code to let users play the game multiple rounds against the computer.
1)Option 1: use a for-loop to let the player play this game 3 times. For each round of the game, display a message indicating whether the player wins, loses, or draws. After the player has played 3 times, display a message indicating how many times the player has won.
2)Option 2: use a while loop to let the player play this game multiple times and the loop ends when the player wins 3 times. For each round of the game, display a message indicating whether the player wins, loses, or draws. After the player has won 3 times, display a message informing the player that the player has won 3 out of how many times played.
3)For either option, within each round of the game:
a. Let the computer randomly choose scissors, rock, and paper.
b.Prompt the player to make the player’s choice.
c.Call the play_game function to play the game once.
3. Call the main function and test your code.
Here our task is to implement a ROCK-PAPER-SCISSOR game which can be played agains computer
Things to remeber before coding
Now let's see How to code this in python
CODE
(Please read all comments for the better understanding of the program)
Sample Run
I am also attching the text version of the code in case you need to copy paste
import random #importing random package
def play_game(user,computer): #function to check whether user or
computer wins
if (user == 1): #nested if cases to check all possibilities
if(computer == 2):
return 0
elif(computer == 3):
return 1
elif (user == 2):
if(computer == 1):
return 1
elif(computer == 3):
return 0
elif (user == 3):
if(computer == 2):
return 1
elif(computer == 0):
return 0
if __name__=="__main__": #main function starts here
win=0 #varaible to store counr for win
lst=["You LOSE","You WON","Draw"] #A list used to store
results
for i in range(3): #option 1
user=int(input("paper(1)__Scissor(2)__Rock(3) :")) #geting input
from user and stores to user
computer=random.randint(1,3) #generating random varable and stores
to vaiable computer
print(computer) #printing the computers choice
if (user != computer): #checking for draws
y=play_game(user,computer) #calling play_game functions
print(lst[y])
win=win+y #count increasing if user wins
else:
print(lst[2])
print("You won",win,"times")