2014-12-31 38 views
-3

我創建了一個類似於電視及其遙控器的程序。但是當我運行代碼並嘗試打開電視作爲第一選擇時,它顯示'AttributeError:can not set attribute'。這是代碼:Python在__init__方法中沒有設置屬性

class Television(object): 
    """A TV.""" 

    def __init__(self, channel=1, volume=10): 
     self.channel=channel 
     self.volume=volume 

    def __str__(self): 
     data="\nChannel : "+str(self.channel)+"\nVolume : "+str(self.volume) 
     return data 

    def __set_channel(self, channel): 
     if channel==self.channel: 
      print("\nYou are already watching channel ", channel) 
     else: 
      self.channel=channel 
      print("\nYou are now watching channel ", channel) 
    channel=property(__set_channel) 

    def __set_volume(self, volume): 
     self.volume=volume 
     print("\nYour current volume is ", volume) 
    volume=property(__set_volume) 

class Remote(object): 
    """A remote control for the TV.""" 

    def __get_channel(self): 
     channel=input("\nEnter the channel number : ", channel) 
     if not channel: 
      print("\nYou have to enter a channel number.") 
     else: 
      try: 
       int(channel) 
      except(ValueError): 
       print("\n", channel, " is not a valid channel.") 
      else: 
       if channel not in range(1, 101): 
        print("\n", channel, " is not in range.") 
       else: 
        return channel 
    channel=property(__get_channel) 

    def __volume_up(self, volume): 
     volume+=1 
     if volume>100: 
      print("\nYou are currently at maximum volume.") 
     else: 
      return volume 

    def __volume_down(self, volume): 
     volume-=1 
     if volume<0: 
      print("\nYou are currently at maximum volume.") 
     else: 
      return volume 

class OFF(object): 
    """A TV at off state.""" 



def get_choice(state): 
    """Gets a choice from the user.""" 

    if state==0: 
     print(\ 
      """ 
      MUATHASIM TV 

      0 - Quit 
      1 - Switch the TV on 
      """) 
    else: 
     print(\ 
      """ 
      MUATHASIM TV 

      0 - Quit 
      1 - Switch off the TV 
      2 - Change the channel 
      3 - Turn up the volume 
      4 - Turn down the volume 
      """) 

    choice=input("\nChoice : ") 
    return choice 

def main(): 
    """Main part of the program.""" 

    remote=Remote() 
    choice=None 
    state=0 
    while choice!="0": 
     choice=get_choice(state) 

     if state==0: 
      if choice=="0": 
       print("\nGood Bye!") 
      elif choice=="1": 
       TV=Television() 
      else: 
       print("\n", choice, " is not a valid choice.") 
     elif state==1: 
      if choice=="0": 
       print("\nGood Bye!") 
      elif choice=="1": 
       TV=OFF() 
      elif choice=="2": 
       channel=remote.channel 
       TV.channel=channel 
      elif choice=="3": 
       volume=remote.__volume_up(TV.volume) 
       TV.volume=volume 
      elif choice=="4": 
       volume=remote.__volume_down(TV.volume) 
       TV.volume=volume 
      else: 
       print("\n", choice, "is not a valid choice.") 




main() 

input("\n\n\nPress the Enter key to exit.") 
+3

你可以添加完整的堆棧跟蹤(完整的錯誤輸出)到你的問題? – Dettorer

+1

也請參閱http://stackoverflow.com/help/mcve – jonrsharpe

+0

問題是行'channel = property(__ set_channel)'。刪除(和相應的「音量」線),錯誤消失。我不打算研究代碼中其他地方有什麼影響;是否刪除/縮進/移動該行取決於您。 – alexwlchan

回答

4

的問題是你創建你的屬性的方式: channel=property(__set_channel)創建與getter函數__set_channel屬性。 你必須創建一個適當的getter函數,例如:

def __get_channel(self): 
    return self._channel 

,創造財產

channel= property(__get_channel, __set_channel) 

最後,你需要修復您的setter函數:self.channel=channel將調用setter函數,產生在無限循環中。改爲使用self._channel=channel

相關問題