Question: I need help with this python problem. Your traffic light controller program has been patented, and you are about to become seriously rich. But your
I need help with this python problem. Your traffic light controller program has been patented, and you are about to become seriously rich. But your new client needs a change. They want four states in the state machine: Green, then Green and Orange together then Orange only, and then Red. Additionally, they want different times spent in each state. The machine should spend 3 seconds in the Green state, followed by one second in the Green+Orange state, then one second in the Orange state, and then 2 seconds in the Red state. Change the logic in the state machine.
Here is the code I have so far
import turtle
wn = turtle.Screen() wn.title("traffic light!") wn.bgcolor("lightgreen")
def draw_housing(): house = turtle.Turtle() house.pensize(3) house.color("black", "darkgrey") house.begin_fill() house.forward(80) house.left(90) house.forward(200) house.circle(40, 180) house.forward(200) house.left(90) house.end_fill()
# This function will draw 3 black lights # which will work as off signal lights def draw_off_lights(): distance = 50 for i in range(3): off_turtle = turtle.Turtle() off_turtle.speed(0) off_turtle.penup() off_turtle.forward(40) off_turtle.left(90) off_turtle.forward(distance) off_turtle.shape("circle") off_turtle.shapesize(3) off_turtle.fillcolor("#333") distance+=70
draw_housing() draw_off_lights()
first = turtle.Turtle() first.penup() # Position first onto the place where the green light should be first.forward(40) first.left(90) first.forward(50) first_pos = first.position() # Turn first into a big green circle first.shape("circle") first.shapesize(3) first.fillcolor("green")
second = turtle.Turtle() second.penup() # Position second onto the place where the green yellow should be second.forward(40) second.left(90) second.forward(120) second_pos = second.position() # Turn second into a big yellow circle second.shape("circle") second.shapesize(3) second.fillcolor("yellow")
third = turtle.Turtle() third.penup() # Position third onto the place where the red light should be third.forward(40) third.left(90) third.forward(190) third_pos = third.position() # Turn third into a big red circle third.shape("circle") third.shapesize(3) third.fillcolor("red")
# This variable holds the current state of the machine state_num = 0
def advance_state_machine1(): global state_num
if state_num == 0: # Transition from state 0 to state 1 state_num = 1 first.showturtle() second.hideturtle() third.hideturtle() elif state_num == 1: # Transition from state 1 to state 2 state_num = 2 first.hideturtle() second.showturtle() third.hideturtle() else: # Transition from state 2 to state 0 state_num = 0 first.hideturtle() second.hideturtle() third.showturtle()
wn.ontimer(advance_state_machine1, "5000")
advance_state_machine1()
wn.mainloop()
By the way the version of python I'm using is python 3.6
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
