Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Enhance the Dice Roller program

Open and test the program

  1. In IDLE, open the dice.py and dice.roller.py files that are in your student download python/exercises/ch14/dice_roller
  2. 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)

  1. 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
  2. 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.
  3. In the Die class, modify the setter for the value property so it doesnt allow a value greater than 6.
  4. 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)

  1. Open the dice_roller.py file and run it to make sure the Dice Roller program works correctly.
  2. 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.
  3. 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)

  1. In the Dice class, add a method named getTotal() that gets the total value of all die objects currently stored in the Dice object.
  2. In the Dice class, add a method named getAvg() that gets the average value of all die objects currently stored in the Dice object.
  3. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

What applied experiences do you have? (For Applied Programs Only)

Answered: 1 week ago