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

Write a program that simulates a television with a class Television. The class s

ID: 3652426 • Letter: W

Question

Write a program that simulates a television with a class Television. The class should create the following attributes for a new object:

_channel for the channel to which the television is tuned

volume for the volume level of the television

is_on for whether or not the television is on

The class should define the following methods:

_intit_() should create and initialize an object's three attributes: _channel, volume and is_on

_str_() should return a string that describes the status of the television; if the television is on, the string should indicate that the television is on and list its channel number and volume level; otherwise, the string the string should indicate that the television is off

toggle_power() should turn the television on it its off or off if its on

get_channel() should return the channel number to which the television is tuned

set_channel() should receive a channel number and change the channel to this number if the television is on and the channel number is between 0 and 499, the minimum and maximum channel values; otherwise, the method should display a message saying why the attempt to change channels was unsuccessful

raise_volume() should increase the volume level if the television is on and the volume level isn't already 10, the maximum volume level; otherwise the method should display a message saying why the attempt to raise the volume level was unsuccessful.

lower_volume() should decrease the volume level if the television os on and the volume level isnt already -, the minimum volume level; otherwise, the method should display a message saying why the attempt to lower the volume was unsuccessful.

The class should define one property:

channel for the channel to which the television is tuned, using the get_channel() and set_channel() methods

Your program should instantiate an object from the class Television and allow a user to manipulate it through a menu system. The menu choices should be:

0 - Exit
1 - Toggle Power
2 - Change Channel
3 - Raise Volume
4 - Lower Volume

The Program should display the status of the television each time it presents the menu

Explanation / Answer

Here is the code I wrote after reading your problem definition. Dont forget to rate.







class Television(object):

def __init__(self, channel=1, volume=1, is_on=False):

self._channel= channel

self.volume = volume

self.is_on = is_on

def __str__(self):

volume = self.volume

if not volume:

volume = 'muted'

elif volume == 10:

volume = 'max'

if self.is_on:

return "The TV is on, channel {0}, volume {1}".format(self.channel, volume)

else:

return "The TV is off."

def toggle_power(self):

self.is_on = not self.is_on

return self.is_on

def get_channel(self):

return self._channel

def set_channel(self, choice):

self._check_on()

if 0 <= choice <= 499:

self._channel = choice

else:

raise ValueError('Invalid channel')

channel = property(get_channel, set_channel)

def _check_on(self):

if not self.is_on:

raise ValueError("The television isn't on")

def raise_volume(self, up=1):

self._check_on()

self.volume += up

if self.volume >= 10:

self.volume = 10

def lower_volume(self, down=1):

self._check_on()

self.volume -= down

if self.volume <= 0:

self.volume = 0

def main():

tv = Television()

while True:

print 'Status:', tv

print

"""

Television

0 - Exit

1 - Toggle Power

2 - Change Channel

3 - Raise Volume

4 - Lower Volume

"""

choice=raw_input("Choice: ")

try:

if choice=="0":

break

elif choice=="1":

tv.toggle_power()

elif choice=="2":

change=int(raw_input("What would you like to change the channel to? "))

tv.set_channel(change)

elif choice=="3":

tv.raise_volume()

elif choice=="4":

tv.lower_volume()

else:

raise ValueError("Sorry, but {0} isn't a valid choice.".format(choice))

except ValueError as e:

print ' *** ERROR: {0} '.format(e)

main()

raw_input("Press enter to exit.")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote