class Television: """ Model for a typical television object. """ def __init__(self): """Create a new Television instance. The power is initially off. The first time the TV is turned on, it will be set to channel 2 with a volume level of 5 (unmuted). """ self._powerOn = False self._muted = False self._volume = 5 self._channel = 2 self._prevChan = 2 def togglePower(self): """Toggle the power setting. If Television is off, turn it on. If Television is on, turn it off. """ self._powerOn = not self._powerOn def toggleMute(self): """Toggle the setting for mute. If power is off, there is no effect. Otherwise, if television was unmuted, it becomes muted. If television was muted, it becomes unmuted and the volume is restored to its previous setting. """ if self._powerOn: self._muted = not self._muted def volumeUp(self): """Increment the volume of the Television by one increment. If power is currently off, there is no effect. Otherwise, update the volume setting appropriately, and return the new value to the caller. If television is currently muted, it will be unmuted as a result. If volume was at maximum level, it remains at maximum level. """ if self._powerOn: if self._volume < 10: self._volume += 1 self._muted = False return self._volume def volumeDown(self): """Decrement the volume of the Television by one increment. If power is currently off, there is no effect. Otherwise, update the volume setting appropriately, and return the new value to the caller. If television is currently muted, it will be unmuted as a result. If volume was at minimum level, it remains at minimum level. """ if self._powerOn: if self._volume > 0: self._volume -= 1 self._muted = False return self._volume def channelUp(self): """Increment the channel and return the resulting setting. If power is off, there is no effect. If channel had been set to the maximum of the valid range of channels, the effect is to wrap around to the minimum channel. """ if self._powerOn: self._prevChan = self._channel # record the current value if self._channel == 99: self._channel = 2 # wrap around to minimum else: self._channel += 1 return self._channel def channelDown(self): """Decrement the channel and return the resulting setting. If power is off, there is no effect. If channel had been set to the minimum of the valid range of channels, the effect will be to 'wrap' around to the maximum channel. """ if self._powerOn: self._prevChan = self._channel # record the current value if self._channel == 2: self._channel = 99 # wrap around to maximum else: self._channel -= 1 return self._channel def setChannel(self, number): """Set the channel to given value (if valid channel). If power is off, there is no effect. """ if self._powerOn: if 2 <= number <= 99: self._prevChan = self._channel # record the current value self._channel = number return self._channel def jumpPrevChannel(self): """Change the channel to be that of the most recent other channel to have been viewed. Return the resulting channel setting. If power is off, there is no effect. """ if self._powerOn: incoming = self._channel self._channel = self._prevChan self._prevChan = incoming return self._channel def __str__(self): """Return a str object representing the current observable settings.""" display = 'Power setting is currently ' + str(self._powerOn) + '\n' display += 'Channel setting is currently ' + str(self._channel) + '\n' display += 'Volume setting is currently ' + str(self._volume) + '\n' display += 'Mute is currently ' + str(self._muted) return display def isOn(self): """Return True if power is currently on, and False otherwise. This exists for testing, and so it works even when power is off. """ return self._powerOn def isMuted(self): """Return True if currently muted, and False otherwise. This exists for testing, and so it works even when power is off. """ return self._muted def getChannel(self): """Return the current channel number. This exists for testing, and so it works even when power is off. """ return self._channel def getVolume(self): """Return the current volume level. This exists for testing, and so it works even when power is off. """ return self._volume