如何打开和关闭更改开关的类(SwitchBoard)?

-4 python boolean class function

我想用以下属性构建一个类SwitchBoard:

•当我创建交换机时,我应该能够设置它包含的交换机数量.

•所有开关应从"关闭"位置开始.

•如果我打印配电盘,它应该按以下方式打印:"以下开关打开:0 2 4 6 8".

•哪种切换方法应按顺序返回表示开启的整数列表(例如,[1,3,5,7,9]).

•如果我用n作为整数调用flip(n),它应该翻转第n个lightswitch的状态.

•如果我用n作为整数调用每个(n)翻转,它应该翻转每个第n个光开关的状态,从0开始.所以翻转每(2)将翻转开关0,2,4,6等.

•方法reset()应关闭所有开关.

•如果我要求交换机翻转不存在的交换机,则不应发生任何事情(它不应该崩溃)

所以,我首先构建一个运行良好的Lightswitch.

class LightSwitch():
''' A class to reprenset a general light switch'''

    def __init__(self, light_state):
        if(light_state == 'on'):
            mode = True
        else:
            mode = False
        self._state = mode

    def __str__(self):
        if(self._state == True):
           return 'I am on'
        if(self._state == False):
            return 'I am off'

    def turn_on(self):
        if(self._state == False):
            self._state = not self._state

    def turn_off(self):
        if(self._state == True):
            self._state = not self._state

    def flip(self):
        self._state = not self._state
Run Code Online (Sandbox Code Playgroud)

对于SwitchBoard来说,这就是我所拥有的.

class SwitchBoard():

    def __init__(self, num_switch):
        self.switches = []
        for i in range(num_switch):
            self.switches.append(LightSwitch('off'))

    def __str__(self):
        switch_on = ''
        switch_on_list = self.which_switch()
        for i in range(0, len(switch_on_list)):
            switch_on += ' ' + str(switch_on_list[i])
        return 'The following switches are on:' + switch_on

    def which_switch(self):
        switch_on_list = []
        for i in range(0, len(self.switches) - 1):
            if(self.switches[i] == True):
                switch_on_list.append(i)
        return switch_on_list

    def flip(self, switch_index):
        if(switch_index <= len(self.switches) -1 ):
        self.switches[switch_index].flip()

    def flip_every(self, step):
        for i in range(0,len(self.switches), step):
            self.flip(i)        

    def reset(self):
        for every_switch in self.switches:
            every_switch.turn_off()


if(__name__==("__main__")):
s = SwitchBoard(10)
s.flip(2)
print(s)
print(s.which_switch())
s.flip_every(2)
print(s)
print(s.which_switch())  
Run Code Online (Sandbox Code Playgroud)

这没有用.翻转似乎不起作用,我不知道为什么.请帮忙!

A.H*_*A.H 10

试试吧

def which_switch(self):
    switch_on_list = []
    for i in range(0, len(self.switches) - 1):
        if(self.switches[i]._state == True):
            switch_on_list.append(i)
    return switch_on_list
Run Code Online (Sandbox Code Playgroud)

但实际上,你应该为你的LightSwitch添加一个getter ,并使用这个getter,因为访问一个下划线的变量不是很干净...例如,你可以添加:

is_on(self):
    return self._state
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

if(self.switches[i].is_on()):
Run Code Online (Sandbox Code Playgroud)

但是,很棒的代码,只是有点想念.

顺便说一句,你还有一些错误:

if(switch_index <= len(self.switches) -1 ):
Run Code Online (Sandbox Code Playgroud)

不干净,因为你从不禁止负数例如.

  • 我同意......即将发布相同的内容.其中一些for..range()循环有问题..在大多数情况下,使用迭代器迭代python列表更好,例如`for list in list_of_switches:`...更不容易出错 (2认同)