I'm making a simulated tv
I need to make a tv that shows the user the channel and the volume, and shows whether or not the television is on. I have the majority of the code made, but for some reason the channels won't switch. I'm fairly unfamiliar with how properties work, and I think that's what my problem here is. Help please.
class Television(object):
def __init__(self, __channel=1, volume=1, is_on=0):
self.__channel=__channel
self.volume=volume
self.is_on=is_on
def __str__(self):
if self.is_on==1:
print "The tv is on"
print self.__channel
print self.volume
else:
print "The television is off."
def toggle_power(self):
if self.is_on==1:
self.is_on=0
return self.is_on
if self.is_on==0:
self.is_on=1
return self.is_on
def get_channel(self):
return channel
def set_channel(self, choice):
if self.is_on==1:
if choice>=0 and choice<=499:
channel=self.__channel
else:
print "Invalid channel!"
else:
print "The televis开发者_如何学Goion isn't on!"
channel=property(get_channel, set_channel)
def raise_volume(self, up=1):
if self.is_on==1:
self.volume+=up
if self.volume>=10:
self.volume=10
print "Max volume!"
else:
print "The television isn't on!"
def lower_volume(self, down=1):
if self.is_on==1:
self.volume-=down
if self.volume<=0:
self.volume=0
print "Muted!"
else:
print "The television isn't on!"
def main():
tv=Television()
choice=None
while choice!="0":
print \
"""
Television
0 - Exit
1 - Toggle Power
2 - Change Channel
3 - Raise Volume
4 - Lower Volume
"""
choice=raw_input("Choice: ")
print
if choice=="0":
print "Good-bye."
elif choice=="1":
tv.toggle_power()
tv.__str__()
elif choice=="2":
change=raw_input("What would you like to change the channel to?")
tv.set_channel(change)
tv.__str__()
elif choice=="3":
tv.raise_volume()
tv.__str__()
elif choice=="4":
tv.lower_volume()
tv.__str__()
else:
print "\nSorry, but", choice, "isn't a valid choice."
main()
raw_input("Press enter to exit.")
The channel number is integer, but raw_input returns string. Should be:
change = int(raw_input("What would you like to change the channel to?"))
also your
set_channel
function has this:channel=self.__channel
When it should be:
self.__channel = choice
Those two changes make it work.
EXTRA HINTS:
Posting this as community wiki so everybody can help with ideas and hints.
- Don't name your attributes starting with two underscores. It doesn't mean private. If you want some private name, use a single underscore.
- You're not really using the
channel
property you created. - Use True and False instead of 1 and 0 for the
is_on
__str__
method should return a string, not print one. And then you don't invoke it directly, you just print the instance and it will be invoked by python (usually you don't invoke methods that start and end with two underscores yourself)
Code using hints above:
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 '\n\n *** ERROR: {0}\n'.format(e)
main()
raw_input("Press enter to exit.")
精彩评论