Question
Enhance the Dice Roller program Open and test the program In IDLE, open the dice.py and dice.roller.py files that are in your student download python/exercises/ch14/dice_roller
Open and test the program
- In IDLE, open the dice.py and dice.roller.py files that are in your student download python/exercises/ch14/dice_roller
- Review the code and run the program. Note that it starts by displaying an image for each of the 6 possible die values.
Improve the Die class (4 points for each item)
- In the Die class, modify the roll() method so it returns the __value attribute after it sets it to a random number from 1 to 6
- In the Die class, modify the constructor so it sets the __value attribute by calling the roll() method. This makes sure that the __value attribute for a new Die object stores a valid number for the die.
- In the Die class, modify the setter for the value property so it doesnt allow a value greater than 6.
- In the Die class, add a read-only property named image that gets a string for the image that represents the dies current value.
Improve the Dice Roller program (3 points for each item)
- Open the dice_roller.py file and run it to make sure the Dice Roller program works correctly.
- Modify the code that displays the roll so it uses the new image property to display an image for the die instead of displaying the value.
- At the start of the program, modify the code that displays the 6 die images so it uses a loop to create a Die object for each valid number and to display its image.
Improve the Dice class (3 points for each item)
- In the Dice class, add a method named getTotal() that gets the total value of all die objects currently stored in the Dice object.
- In the Dice class, add a method named getAvg() that gets the average value of all die objects currently stored in the Dice object.
- In the dice_roller.py file, add the code that displays the total and the average each time the user rolls the dice.
(dice roller program)
from dice import Dice, Die
def main():
print(\"The Dice Roller program\")
print(\" _____ \" + \\
\"|o o| \" + \\
\"|o o| \" + \\
\"|o___o|\")
print(\" _____ \" + \\
\"|o o| \" + \\
\"| o | \" + \\
\"|o___o|\")
print(\" _____ \" + \\
\"|o o| \" + \\
\"| | \" + \\
\"|o___o|\")
print(\" _____ \" + \\
\"|o | \" + \\
\"| o | \" + \\
\"|____o|\")
print(\" _____ \" + \\
\"|o | \" + \\
\"| | \" + \\
\"|____o|\")
print(\" _____ \" + \\
\"| | \" + \\
\"| o | \" + \\
\"|_____|\")
print()
# get number of dice from user
count = int(input(\"Enter the number of dice to roll: \"))
# create Die objects and add to Dice object
dice = Dice()
for i in range(count):
die = Die()
dice.addDie(die)
while True:
# roll the dice
dice.rollAll()
print(\"YOUR ROLL: \", end=\"\")
for die in dice.list:
print(die.value, end=\" \")
print(\" \")
choice = input(\"Roll again? (y/n): \")
if choice != \"y\":
print(\"Bye!\")
break
if __name__ == \"__main__\":
main()
(dice program)
import random
class Die:
def __init__(self):
self.__value = 0
@property
def value(self):
return self.__value
@value.setter
def value(self, value):
if value
raise ValueError(\"Die can't be less than 1.\")
else:
self.__value = value
def roll(self):
self.__value = random.randrange(1, 7)
class Dice:
def __init__(self):
self.__list = []
def addDie(self, die):
self.__list.append(die)
@property
def list(self):
dice_tuple = tuple(self.__list)
return dice_tuple
def rollAll(self):
for die in self.__list:
die.roll()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started