Editor

Create a Traffic Light Enum

Intermediate · Write the code · from Python Enums: Type-Safe Constants for Cleaner Code

From the lesson: Python Enums: Type-Safe Constants for Cleaner Code

Imagine you're building a card game. You need to represent the four suits: hearts, diamonds, clubs, and spades. You could use plain strings like "hearts", but what happens when someone types "Hearts" or "hart"? Your code breaks silently.

Read the lesson

Create an Enum called TrafficLight with members RED, YELLOW, and GREEN having string values "stop", "caution", and "go". Then print each member's name and value on separate lines in the format NAME: value.

from enum import Enum
# Define TrafficLight enum here
# Print each member

More Enum exercises

All Python exercises · 50 practice problems · Standard Library Essentials quiz