Help With Better Design
apollonius2 at gmail.com
apollonius2 at gmail.com
Tue Jun 19 21:34:27 EDT 2007
Greetings,
I have been working on a little project today to help me better
understand classes in Python (I really like Python). I am a self
taught programmer and consider myself to fall in the "beginner"
category for sure. It was initially sparked by reading about "state
machines". This was my attempt at it however I feel it is not quite
where it should be:
ON = "ON"
OFF = "OFF"
class LightBulb:
def __init__(self, initial_state):
self.state = initial_state
def TurnOn(self):
if self.state == OFF:
self.state = ON
else:
print "The Bulb Is Already ON!"
def TurnOff(self):
if self.state == ON:
self.state = OFF
else:
print "The Bulb Is Aleady OFF!"
if __name__== "__main__":
light = LightBulb(OFF)
simulation_running = True
while simulation_running:
print "The light is", light.state
print ""
print "Please choose an action:"
print ""
print "[1] Turn Light On"
print "[2] Turn Light Off"
print "[3] Exit Simulation"
print ""
u_choice = raw_input("Please Enter Your Choice: ")
if u_choice == '1':
light.TurnOn()
if u_choice == '2':
light.TurnOff()
elif u_choice == '3':
break
else:
continue
The test portion of the code is actually longer than the class
itself :-) I would like to be able to get a good hold of the concept
with this example before I try to model a more complex problem. Would
someone be willing to give me some feedback on this class and whether
I am on the right track or how I might better go about it?
More information about the Python-list
mailing list