Traffic Light

Tutorial for Python Enum: Building a Traffic Light Simulator

In this tutorial for Python Enum, we’ll explore the use of Python’s enum module by building a simple traffic light simulator. Python Enums (enumerations) are a way to name constant values, making your code more readable and self-explanatory. They are particularly useful when you have a set of named constants like directions, states, or types.

Table of Contents

  1. Getting Started
  2. Creating an Enum Class
  3. Traffic Light Simulator
  4. User Interface
  5. Putting It All Together
Tutorial for Python Enum

1. Getting Started

First, make sure Python is installed on your system. Then, you can use the built-in enum module without needing to install any additional packages.

2. Creating a Python Enum Class

Let’s create an Enum class for the traffic light states: Red, Yellow, and Green.

Python
from enum import Enum

class TrafficLight(Enum):
    RED = 1
    YELLOW = 2
    GREEN = 3

In this example, RED, YELLOW, and GREEN are enum members and are functionally constants.

3. Traffic Light Simulator

Now, we can use this TrafficLight enum to build our traffic light simulator.

Python
def traffic_light_simulator(state):
    if state == TrafficLight.RED:
        print("Stop")
    elif state == TrafficLight.YELLOW:
        print("Slow down")
    elif state == TrafficLight.GREEN:
        print("Go")
    else:
        print("Invalid state")

Example:

Python
traffic_light_simulator(TrafficLight.RED)  # Output will be "Stop"

4. User Interface

Let’s create a simple user interface to interact with our simulator.

Python
def user_interface():
    while True:
        print("\nTraffic Light Simulator")
        print("1: Red")
        print("2: Yellow")
        print("3: Green")
        print("4: Exit")

        choice = input("Choose a traffic light state (1/2/3/4): ")

        if choice == '1':
            traffic_light_simulator(TrafficLight.RED)

        elif choice == '2':
            traffic_light_simulator(TrafficLight.YELLOW)

        elif choice == '3':
            traffic_light_simulator(TrafficLight.GREEN)

        elif choice == '4':
            print("Exiting the simulator.")
            break

        else:
            print("Invalid choice.")

Example:

Python
user_interface()

5. Putting It All Together

Here’s the complete code combining all the parts.

Python
from enum import Enum

# Enum Class
class TrafficLight(Enum):
    RED = 1
    YELLOW = 2
    GREEN = 3

# Traffic Light Simulator Function
def traffic_light_simulator(state):
    if state == TrafficLight.RED:
        print("Stop")
    elif state == TrafficLight.YELLOW:
        print("Slow down")
    elif state == TrafficLight.GREEN:
        print("Go")
    else:
        print("Invalid state")

# User Interface
def user_interface():
    while True:
        print("\nTraffic Light Simulator")
        print("1: Red")
        print("2: Yellow")
        print("3: Green")
        print("4: Exit")

        choice = input("Choose a traffic light state (1/2/3/4): ")

        if choice == '1':
            traffic_light_simulator(TrafficLight.RED)

        elif choice == '2':
            traffic_light_simulator(TrafficLight.YELLOW)

        elif choice == '3':
            traffic_light_simulator(TrafficLight.GREEN)

        elif choice == '4':
            print("Exiting the simulator.")
            break

        else:
            print("Invalid choice.")

# Run the User Interface
user_interface()

Congratulations! You’ve now created a simple traffic light simulator using Python’s enum module. This tutorial for python enum, demonstrates how enums can make your code more readable and maintainable. Feel free to extend this example to include more complex behaviors, like transitioning between states automatically.