Menu
CoddyTech

Recap - Traffic Light

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 41 of 78.

challenge icon

Challenge

Easy

Simulate a traffic light. At the top level of the script, declare an enum named TrafficLight with the options RED, YELLOW and GREEN, and write three functions that each use a match on the light:

  • get_driver_action(light) returns "Stop" for red, "Caution" for yellow and "Go" for green
  • get_wait_time(light) returns 30 for red, 3 for yellow and 25 for green
  • get_next_light(light) returns the light that comes next: red turns green, green turns yellow and yellow turns red

The program reads two lines: the name of the first light (RED, YELLOW or GREEN) and how many lights to show (a whole number, 1 or more). For each light, print its name, its action and its wait time, then move to the next light. Last, print the sum of all the wait times.

For the inputs RED and 4, the output is:

RED: Stop for 30 seconds
GREEN: Go for 25 seconds
YELLOW: Caution for 3 seconds
RED: Stop for 30 seconds
Total wait: 88 seconds

Try it yourself

extends Node

# Declare the TrafficLight enum here

# Write get_driver_action(), get_wait_time() and get_next_light() here

func _ready():
	var first := OS.read_string_from_stdin()
	var count := int(OS.read_string_from_stdin())
	# Show the lights and the total wait time
	pass

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler