2010-03-23 324 views
1

我需要製作一個電視,向用戶顯示頻道和音量,並顯示電視是否打開。我有大部分的代碼,但由於某些原因,通道不會切換。我很不熟悉物業是如何運作的,我認爲這就是我的問題所在。請幫助。我正在模擬電視

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 television 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.") 

回答

5
  1. 通道號是整數,但返回的raw_input串。應該是:

    change = int(raw_input("What would you like to change the channel to?")) 
    
  2. 也是你set_channel功能有這樣的:

    channel=self.__channel 
    

    當它應該是:

    self.__channel = choice 
    

這兩個變化,使其工作。

3

EXTRA提示:

發佈這個社區維基所以每個人都可以有想法和提示幫助。

  • 請勿將您的屬性命名爲以兩個下劃線開頭。它意思是私人的。如果您想要某個專用名稱,請使用單個下劃線。
  • 您並未真正使用您創建的channel屬性。
  • 使用True和False代替1和0代替is_on
  • __str__方法應該返回一個字符串,而不是打印一個。然後,你不必直接調用它,你只打印實例,它將被蟒蛇被調用使用(一般你不調用該方法,啓動和結束與兩個下劃線自己)

代碼以上提示:

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.")