Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python enumerations code

Practice defining enum members, looking up a value, iterating over members, and rejecting accidental aliases.

Intermediate ~7 min read

Challenge 1

Define the two named transport choices, using explicit string values.

python Your answer
from enum import Enum

class Transport(Enum):
    

print(Transport.TRAIN.value)

Press Ctrl⌘Enter to check your answer

Challenge 2

Look up the SENT member using its stored value.

python Your answer
from enum import Enum

class Status(Enum):
    READY = "ready"
    SENT = "sent"

current = Status()
print(current is Status.SENT)

Press Ctrl⌘Enter to check your answer

Challenge 3

Complete the loop so it visits every declared level in definition order.

python Your answer
from enum import Enum

class Level(Enum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3

for level in :
    print(level.name.lower())

Press Ctrl⌘Enter to check your answer

Challenge 4

Decorate the enum so duplicate member values are rejected.

python Your answer
from enum import Enum, unique


class Mode(Enum):
    READ = "read"
    WRITE = "write"

print(Mode.READ)

Press Ctrl⌘Enter to check your answer