Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code must be printed in python 3 and document the code by python comment. W

ID: 3789295 • Letter: T

Question

This code must be printed in python 3 and document the code by python comment. Write a program to play a three-round match of “rock, paper, scissors” between a person and a computer. Each player should know his or her name and score (i.e., how many games they won, lost, and tied). After the person makes his or her choice, the computer should make its choice at random. After both choices are made, the program should display who won that round. After all three rounds have been played, print out the final score for each player as well as who won the match. For example, if the person won 2 rounds and the computer won 1 round, the person would win the match.

Your program must:

Store all information and provide all operations listed in the problem description.

Use at least one class (but can use more) - all functions should be written as class methods.

Include a main method where you allow someone to play a three-round match of “rock, paper, scissors.”

Include comments to document your code.

Some hints and reminders:

Rock beats scissors, scissors beats paper, and paper beats rock.

To allow the computer to make its choice at random you will need to use the random library. For example, consider the function random.randint(a,b) which returns a random integer N such that a N b. Could this be useful to determine a random choice for the computer?

Inheritance may be useful (but is not mandatory).

Instead of submitting pseudocode for this assignment, you will submit a program design. Your design should:

Describe all instance variables for the class/classes (including the data type of the variables).

Describe all necessary class methods, including the parameters passed to the methods. To identify what methods to include, think about what operations would be required for the object/objects to meet the specification above.

Describe all accessor/mutator methods necessary for the class (i.e., just list which variables will have a corresponding accessor and/or mutator method).

Note: When describing the class methods for the program design include the name of your method, the parameters that need to be passed to the method, and a brief description of what the method should do. You do not need to write the code/pseudocode for the methods as part of the design!

Explanation / Answer

Solution:

from random import randint
class Game:
def __init__(self):
#take input from user..
self.name = raw_input("your name please")
#assign a random play to the computer
print("Okay..I'm computer..Lets Play")
self.d={self.name: {"win": 0 , "lose":0 , "tie":0}, "computer": {"win": 0 , "lose":0 , "tie":0} }
  
def player_input(self):
self.player = raw_input("Rock, Paper, Scissors?")
  
def computer_input(self):
t = ["Rock", "Paper", "Scissors"]
self.computer=t[randint(0,2)]

def calculation(self):
if self.player == self.computer:
print("Tie!")
self.d[self.name]["tie"]+=1
self.d["computer"]["tie"]+=1
elif self.player == "Rock":
if self.computer == "Paper":
print("You lose!", self.computer, "covers", self.player)
self.d[self.name]["lose"]+=0
self.d["computer"]["win"]+=1
else:
print("You win!", self.player, "smashes", self.computer)
self.d[self.name]["win"]+=1
self.d["computer"]["lose"]+=0
elif self.player == "Paper":
if self.computer == "Scissors":
print("You lose!", self.computer, "cut", self.player)
self.d[self.name]["lose"]+=0
self.d["computer"]["win"]+=1
else:
print("You win!", self.player, "covers", self.computer)
self.d[self.name]["win"]+=1
self.d["computer"]["lose"]+=0
elif self.player == "Scissors":
if self.computer == "Rock":
print("You lose...", self.computer, "smashes", self.player)
self.d[self.name]["lose"]+=0
self.d["computer"]["win"]+=1
else:
print("You win!", self.player, "cut", self.computer)
self.d[self.name]["win"]+=1
self.d["computer"]["lose"]+=0
else:
print("That's not a valid play. Check your spelling..game Abondend!")
exit(0)
  
object=Game()
for i in range(3):
object.player_input()
object.computer_input()
object.calculation()
print("Game Result:")
print("you :" ,object.d[object.name])
print("computer :" ,object.d["computer"])
if object.d["computer"]["win"] > object.d[object.name]["win"]:
print("computer win")
elif object.d["computer"]["win"] == object.d[object.name]["win"]:
print("Match Tie!!")
else:
print("you win")

Sample Output:

explanation:

Class has 4 functions. function __init__() is constuctor which assigns player name and creates a dictionary to store match result. function input_player() takes input from user function input_computer() selects random value from mentioned list. function calculation has a game logic and based on the result it increments the dictionary value. and finally print outs the result.