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 lessonCreate 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
- Predict the Enum OutputIntermediate
- HTTP Status Enum with MethodsIntermediate
- Fix the Enum BugIntermediate
- Build a Permission SystemAdvanced
All Python exercises · 50 practice problems · Standard Library Essentials quiz