Enum
In short: enum.Enum gives a value a fixed, named set of options instead of
a loose string or number — Status.DONE instead of "done", spelled however anyone
felt like that day. A typo in a string silently produces the wrong answer; a typo
in an Enum member name raises AttributeError right where the mistake is. Members
compare by identity (Light.RED is Light.RED), auto() assigns values so you don't
have to pick numbers by hand, and IntEnum additionally behaves like a real int —
comparable, sortable, usable wherever a number is expected. Reach for an Enum the
moment a value can only ever be one of a short, known list.

Order status without enum
A delivery app tracks each order's status as a string. It works — until one line checks the status against a misspelled string:
Nothing crashes. The archive step just silently never runs, and the bug could hide for months. An Enum turns the same typo into an error at the exact line that has it:
from enum import Enum
class Status(Enum):
PENDING = "pending"
DONE = "done"
FAILED = "failed"
print(Status.DNOE)Two things worth knowing up front: an Enum member has both a .name and a .value,
and it does not equal its raw value by default:
Traffic light states
A traffic light only ever has three states, and nobody wants to remember whether
1 means red or green. auto() hands out the numbers so you never have to pick
them by hand:
auto() counts from 1 in definition order — reordering the lines changes the
numbers, so never rely on the exact value if you used auto(), only on the name.
Priority levels that sort
A plain Enum gives you a fixed set, but not an order — two members can't be
compared with < unless you ask for that specifically:
from enum import Enum
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
print(Priority.LOW < Priority.HIGH)IntEnum says so: a member is an int underneath, so ordering, sorting and even
comparing to a plain number all just work:
Quick practice
from enum import Enum
class Suit(Enum):
CLUBS = 1
HEARTS = 2
print(Suit.CLUBS.value == 1, Suit(1) == Suit.CLUBS)from enum import Enum
class Priority(Enum):
LOW = 1
MEDIUM = 2
HIGH = 3
print([p.name for p in Priority])The function is supposed to work with either a string or a Status member. Right now only one of the two calls comes back True. Fix the comparison so both do.
🎯 Your turn
Write next_light(name). name is "RED", "GREEN" or "YELLOW" — the light's
current state. Return the name of the next state in the cycle
RED → GREEN → YELLOW → RED.
next_light("RED")→"GREEN"next_light("YELLOW")→"RED"
Hint: define class Light(Enum): RED = 1; GREEN = 2; YELLOW = 3, keep
ORDER = [Light.RED, Light.GREEN, Light.YELLOW], look up Light[name], find its
index with .index(...), and step forward with % len(ORDER).
Press ▶ Run and hit ✓ Check. ✅
