Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LightSwitch():
- ''' a class representing flipping on/off switches'''
- def __init__(self, default_state):
- ''' (LightSwitch, str) -> None
- Creates a new switch.
- REQ:
- default_state can only be 'on' or 'off'.
- '''
- if default_state == 'on':
- self.state = True
- elif default_state == 'off':
- self.state = False
- def turn_on(self):
- ''' Turns switch on'''
- self.state = True
- def turn_off(self):
- '''Turns switch off'''
- self.state = False
- def flip(self):
- ''' Sets switch to opposite position.'''
- if self.state == True:
- self.state = False
- elif self.state == False:
- self.state = True
- def __str__(self):
- '''Returns string representation of current state of switch.'''
- if self.state == True:
- return 'I am on'
- if self.state == False:
- return 'I am off'
- class SwitchBoard(LightSwitch):
- ''' '''
- def __init__(self, quantity):
- ''' '''
- self.switchboard = []
- self.state = False
- self.quantity = quantity
- for i in range(quantity):
- self.switchboard.append(i)
- def __str__(self):
- self.on_list = []
- for i in range(self.quantity):
- if i == True:
- self.on_list.append(i)
- return("The following switches are on " + str(self.on_list))
- def which_switch(self):
- for i in range(len(self.switchboard)):
- self.on_list = []
- if self.switchboard[i] == True:
- on_list.append(i)
- print(on_list)
- def flip(self, n):
- if self.switchboard[n] == True:
- self.switchboard[n] = False
- else:
- self.switchboard[n] = True
- def flip_every(self, n):
- for i in range(0, len(self.switchboard), n):
- if self.switchboard[n] == True:
- self.switchboard[n] = False
- else:
- self.switchboard[n] = True
- def reset(self):
- for i in range(len(self.switchboard)):
- self.switchboard[i] == False
- s1 = SwitchBoard(10)
- print(s1)
Add Comment
Please, Sign In to add comment