2016-11-18 3 views
-1

私はライトスイッチをオンとオフに切り替えるスイッチボードクラスを作成しています。私はコードを書く方法を考え出しましたが、私のオブジェクトを作成して印刷すると、それは印刷されません。私は何が間違っているのかわからないし、なぜreturn文の代わりにNoneを出力するのですか? "次のスイッチはオンになっています:"スイッチが1つでもない場合でも、上記の文字列を出力する必要があります。誰かが私が印刷したい文字列ではなく、なぜそれが印刷されているのかを知る手助けをすることができますか?クラスは印刷を保持しません

def SwitchBoard(LightSwitch): 
    '''This class will create a switch board for the user to use with n 
    number of switches that is entered by the user''' 

    def __init__(self, num_switches): 
     ''' 
     (SwitchBoard, int) -> NoneType 
     Given a value by the user, the function will create a switch board 
     ''' 
     # create an empty list to be the switchboard with the desired amount 
     # of switches 
     self._listsw = [] 
     # turn each switch to off 
     for index in range(num_switches): 
      self._listsw.append(0) 


    def __str__(self): 
     ''' 
     (SwitchBoard) -> str 
     Returns in a string the switches on the switchboard that are on 
     ''' 
     # create an empty string to store the on switches in 
     result = "" 
     for index in range(len(self._listsw)): 
      if(self._listsw[index] == 1): 
       result += str(index) 
       result += " " 
     return "The following switches are on: " + result 

def which_switch(self): 
    ''' 
    (SwitchBoard) -> list of int 
    Given a switchboard, the function will check to see which switches are 
    on and will return the following switches in order as a list on integers 
    ''' 
    # create an empty list to store the on switches in 
    on_switches = [] 
    # loop through the list to check each switch 
    for index in range(len(self._listsw)): 
     # check if the switch is on 
     if(self._listsw[index] == 1): 
      # if the switch is on, add it to the list 
      on_switches.append(index) 
    # return the list of on switches 
    return on_switches 


def flip(self, n): 
    ''' 
    (SwitchBoard, int) -> NoneType 
    Given a switch as an integer, the function will flip the state of the 
    desired switch on the switchboard. If it is on, it will be turned off. 
    If it is off, it will be turned on. 
    ''' 
    for index in range(len(self._listsw)): 
     # check if the switch is on 
     if(self._listsw[n] == 1): 
      # if it is on, turn the switch off 
      self._listsw[n] = 0 
     # if the switch is off 
     elif(self._listsw[n] == 0): 
      # turn the switch on 
      self._listsw[n] = 1    


def flip_every(self, n): 
    ''' 
    (SwitchBoard, int) -> NoneType 
    Given an integer n, the function will flip the state of every nth 
    integer. If the integer is on, it will be turned off. If the integer is 
    off, it will be turned on. 
    ''' 
    for index in range(0, len(self._listsw), n): 
     # check if the switch is on 
     if(self._listsw[index] == 1): 
      # if it is on, turn the switch off 
      self._listsw[index] = 0 
     # if the switch is off 
     elif(self._listsw[index] == 0): 
      # turn the switch on 
      self._listsw[index] = 1    


def reset(self): 
    ''' 
    (SwitchBoard) -> NoneTypen 
    When called, the function will reset the entire switchboad and all 
    switches will be turned off. 
    ''' 
    # go through the switchboard 
    for index in range(len(self._listsw)): 
     # and turn every switch off 
     self._listsw[index] = 0 
+2

あなたのコードをあなたの質問に貼り付けてください。 –

+0

私はスクリーンショットを投稿しました。「これまでのコード」 –

+1

はい、コピー貼りははるかに優れています。 – Gormador

答えて

2

非常に単純なエラー:クラスを関数として定義しています。

def SwitchBoard(LightSwitch): 

class SwitchBoard(LightSwitch): 

でそして、あなたが行ってもいいです:

を交換してください。現在のところ、SwitchBoardは、何も返さない1つの引数を持つ関数として扱われます。したがって、Noneです。

+0

くそ、それを逃しました。 – Gormador

+0

@ shamanth-chedde、 最初に添付された画像には表示されないので、貼り付けられた関連するコードのコピー全体を優先する例があります;-) – Gormador

+0

oh jeez Idkどのように私はそれを逃したように大丈夫! –

関連する問題