Complete all tasks

Setting Up DicePile
This is the code for the DicePile class



import random # we'll need the random module to roll the dice





class DicePile:

def __init__(self, initQuantity=2, initSides=6): # constructor

self.setQuantity(initQuantity)

self.setSides(initSides)

self.__results = None # results attribute defaults to None



# The attribute should be private, and named rollCount

# When a DicePile object is created, the constructor should set rollCount to 0

self.__rollCount = 0



def __str__(self): # generate a string representation of the object

if self.__results is not None:

resultString = str(self.__results)

else:

resultString = "not rolled"

dicescription = str(self.__quantity) + "d" + str(self.__sides)



# Modify the "__str__" method so that it includes the rollCount

rollCountString = ' (roll count: {})'.format(self.__rollCount)



return dicescription + ": " + resultString + rollCountString



def roll(self):

# Every time the "roll()" method is called, increment rollCount by 1

# Careful that you don't add this statement to the loop body in "roll()"!

self.__rollCount += 1

self.__results = [] # set results to empty list



# generate random numbers and add to results list

for i in range(self.__quantity):

self.__results.append(random.randint(1, self.__sides))





def getQuantity(self): # get quantity attribute

return self.__quantity





def getSides(self): # get sides attribute

return self.__sides





def getResults(self): # return the results list (None if not rolled)

return self.__results





def setQuantity(self, newQuantity): # set quantity attribute

if int(newQuantity) < 1:

raise ValueError("dice quantity cannot be less than 1")



else:

self.__quantity = int(newQuantity)

self.__results = None





def setSides(self, newSides): # set sides attribute

if int(newSides) < 2:

raise ValueError("dice sides cannot be less than 2")



else:

self.__sides = int(newSides)

self.__results = None





# Create a "getRollCount()" method that returns rollCount

def getRollCount(self):

return self.__rollCount

Now make another file, paste the following code into it and save it in the same directory:


import Dice # import the module containing the class



myDice = Dice.DicePile(4, 6) # create an object



myDice.roll() # call the roll method



print(myDice) # use the __str__ method



myDice.setQuantity(8) # change the quantity



myDice.setSides(20) # change the sides



print(myDice.getResults()) # notice the results are now None



myDice.roll() # roll the dice again



print(myDice.getResults()) # show the results



# Test your code by editing the testing program - print the entire object to make sure it is working in the "__str__" method,

print(myDice)



# and print the output of the "getRollCount()" method to test that too.

print(myDice.getRollCount())



This is the program that we will use to test and demonstrate the changes we make to the DicePile class throughout the enhancements . The actual code in this program is not overly important - add, change and delete (or just comment it out) as needed to test what you want to test. In the upcoming tasks, we'll enhance the DicePile class to add new features to it.



2.Enhancing DicePile (rolled Attribute)

While it is possible to determine whether a DicePile has been rolled it isn't particularly convenient:

if myDice.getResults() is not None:

print('The dice have been rolled!')



Many classes contain boolean attributes that make it more convenient to determine things like this.

Add a public attribute named "rolled" which follows these rules:

Initialised to False in the constructor
Set to True at the end of the "roll()" method
Set to False at the end of the "setQuantity()" and "setSides()" methods
Implementing this simply involves adding assignment statements to the methods mentioned. The previous snippet of code could now be rewritten as:

if myDice.rolled:

print('The dice have been rolled!')

Once you've made the changes, look at the start of the "__str__" method: You can replace the "if self.__results is not None:" with "if self.rolled:" - yay for consistency!
Test your code by printing the value of the rolled attribute of a DicePile object at various points in the testing program - just after creation, after rolling, after changing the quantity or sides...
Note that since the attribute is public, it is possible for someone to write a value to it, e.g. "dice.rolled = 'Hodor'". Is this a problem? No, not really. There's no reason or benefit to doing so and it wouldn't break anything else in the class