For my school assignment we have to write a program to simmulate a traffic light. I've got the program working the only problem is that the code is a bit of a mess because I don't know how to use classes properly. And the documentation just confuses me (a bit of a noob).
So I just copied the function I wanted to change an variable in multiple times and changed the variables by hand. I know it's possible to do this with classes and just make an instance of this class with the right variable but I can seem to get it to work. Here is a small example in "pseudo code" of what I want to achieve.
This code is a timer that times for how long the traffic light is on. I want to make a class that can accept a side (left,right,front,back) and print this string so I dont have to copy the code and manualy enter the side. But I can just make an instance with one of the sides.
import time
class trafficlight_timer:
def __init__(self, side1):
self.side = side1
def trafficlight(self, side1):
s = 0
b = 5
t_max = 10
print self.side, "is green"
if b > t_max:
b = t_max
while s <= 60:
time.sleep(1)
s += 1
if s == b:
print self.side, "is red"
print self.side, "was green for %d seconds." % s
print ""
s = 65
trafficlight()
instance_left = trafficlight_timer('left')
If someone could point out to me how this should be done I would be very grateful. Many thanks in advance!