Question: Get the class working: from Lamp import Lamp import random def main(): lamp1 = Lamp() print(Lamp1 is +str(lamp1)) lamp = [Lamp() for i in range(30)]

Get the class working:

from Lamp import Lamp

import random

def main():

lamp1 = Lamp()

print("Lamp1 is "+str(lamp1))

lamp = [Lamp() for i in range(30)]

for ll in lamp:

v = random.randint(0,1)

ll.setLamp(v)

count = 1

for i in range(30):

print(lamp[i], end=" ")

if count % 5 == 0:

print()

count += 1

main()

class Lamp:

__isOn = None

def __init__(self,*args):

if len(args)==1:

self.__isOn = args[0]

elif len(args)==0:

self.__isOn = False

else:

raise Exception("Invalid Constructor")

def __str__(self):

out=None

if self.__isOn:

out = "On"

else:

out= "Off"

out = "{:3s} ".format(out)

return out

def turnOn(self):

self.__isOn = True

def turnOff(self):

self.__isOn = False

def flip(self):

self.__isOn = not self.__isOn

def isOn(self):

return self.__isOn

def setLamp(self,isOn):

self.__isOn = isOn

def __eq__(self,other):

return self.__isOn == other.__isOnGet the class working: from Lamp import Lamp import random def main():

Homework D-1: Get the Python Lamp class working. Write a Python program that creates a list of 30 Lamps that are randomly set to either On or Off. Write out all of the Lamps using 5 columns. Then flip the switch on each of the first 10 lamps, turn the next 10 off, and the final 10 on. Write out all the lamps again. Use a display function to write out the list of lamps

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!