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

Need help with Intro to Computer Science 2 (Python) problem: Do not define any v

ID: 3783393 • Letter: N

Question

Need help with Intro to Computer Science 2 (Python) problem:

Do not define any variables in the classes below (e.g. selfx for some variable name x) other than the ones specified in the assignment description. If you believe you can't write a solution without defining extra variables, please ask so that I can give you suggestions. You are absolutely encouraged to use as many local variables (e.g. x for some variable name x) as you desire l. Implement a class Die (that is a subclass of the object class) representing a die and supporting six methods a. init 0 which either takes no parameters or takes an integer representing the number of sides of the die as a parameter. The constructor sets the number of sides of the object to the provided parameter or to the value 6 if no parameter is provided. The constructor also sets a variable representing the side of the die showing, which you can also think of as the current roll of the die, by calling a method from the random module that generates a random integer in the range [1 number of sides] where number of sides is the number of sides passed to the constructor or 6 if o parameter was provided b. get which returns the side of the die currently showing, that is, the current roll of the die object. It does NOT modify the roll but instead simply returns its value c. roll() which simulates a new roll of the die by calling a method from the random module to generate a random integer in the range ri, number of sidesl where number of sides is the number of sides stored in the die object and resets the variable in the object using that randomly generated value d. numSides0 which returns the number of sides of the die e. repr 0 which returns the side of the die currently showing, i e. the current roll of the die as shown in the sample output below f. Str O which returns the side of the die currently showing, i.e. the current roll of the die as shown in the sample output below The following shows how the Die class and its methods could be used

Explanation / Answer

# save it in die.py

from random import randint

class Die:

def __init__(self, num = 6):
self.sides = num
self.face = randint(1,self.sides)

def get(self):
return self.face

def roll(self):
self.face = randint(1,self.sides)
return self.face

def numSides(self):
return self.sides

def __repr__(self):
return "[" + str(self.face) + "]"

def __str__(self):
return "[" + str(self.face) + "]"

# save it in craps.py

from die import Die

class Craps:
def __init__(self):
self.d1 = Die()
self.d2 = Die()
self.initValue = self.d1.get() + self.d2.get()

if (self.initValue == 7) or (self.initValue == 11):
print("Throw total: " + str(self.initValue) + ". You won!")
elif (self.initValue == 2) or (self.initValue == 3) or (self.initValue == 12):
print("Throw total: " + str(self.initValue) + ". You lose!")
else:
print("Throw total: " + str(self.initValue) + ". Throw for a point!")

def forPoint(self):
self.d1.roll()
self.d2.roll()
value = self.d1.get() + self.d2.get()

if value == self.initValue:
print("Throw total: " + str(value) + ". You won!")
elif value == 7:
print("Throw total: " + str(value) + ". You lose!")
else:
print("Throw total: " + str(value) + ". Throw for a point!")

"""

Sample run

>>> from craps import Craps
>>> c= Craps()
Throw total: 7. You won!
>>> c= Craps()
Throw total: 8. Throw for a point!
>>> c.forPoint()
Throw total: 11. Throw for a point!
>>> c.forPoint()
Throw total: 8. You won!

"""