Guest User

Untitled

a guest
Nov 17th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. class LightSwitch():
  2. ''' a class representing flipping on/off switches'''
  3. def __init__(self, default_state):
  4. ''' (LightSwitch, str) -> None
  5.  
  6. Creates a new switch.
  7.  
  8. REQ:
  9. default_state can only be 'on' or 'off'.
  10. '''
  11. if default_state == 'on':
  12. self.state = True
  13. elif default_state == 'off':
  14. self.state = False
  15.  
  16. def turn_on(self):
  17. ''' Turns switch on'''
  18. self.state = True
  19.  
  20. def turn_off(self):
  21. '''Turns switch off'''
  22. self.state = False
  23.  
  24. def flip(self):
  25. ''' Sets switch to opposite position.'''
  26. if self.state == True:
  27. self.state = False
  28.  
  29. elif self.state == False:
  30. self.state = True
  31.  
  32. def __str__(self):
  33. '''Returns string representation of current state of switch.'''
  34. if self.state == True:
  35. return 'I am on'
  36.  
  37. if self.state == False:
  38. return 'I am off'
  39.  
  40.  
  41. class SwitchBoard(LightSwitch):
  42. ''' '''
  43.  
  44. def __init__(self, quantity):
  45. ''' '''
  46. self.switchboard = []
  47. self.state = False
  48. self.quantity = quantity
  49. for i in range(quantity):
  50. self.switchboard.append(i)
  51.  
  52. def __str__(self):
  53. self.on_list = []
  54. for i in range(self.quantity):
  55. if i == True:
  56. self.on_list.append(i)
  57. return("The following switches are on " + str(self.on_list))
  58. def which_switch(self):
  59. for i in range(len(self.switchboard)):
  60. self.on_list = []
  61. if self.switchboard[i] == True:
  62. on_list.append(i)
  63. print(on_list)
  64.  
  65. def flip(self, n):
  66. if self.switchboard[n] == True:
  67. self.switchboard[n] = False
  68. else:
  69. self.switchboard[n] = True
  70.  
  71. def flip_every(self, n):
  72. for i in range(0, len(self.switchboard), n):
  73. if self.switchboard[n] == True:
  74. self.switchboard[n] = False
  75. else:
  76. self.switchboard[n] = True
  77.  
  78. def reset(self):
  79. for i in range(len(self.switchboard)):
  80. self.switchboard[i] == False
  81.  
  82. s1 = SwitchBoard(10)
  83. print(s1)
Add Comment
Please, Sign In to add comment