Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.__isOnimage text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions