Dataclasses, enumerations and types · 12 / 15 Exercise Easy

States with Enum

Objective

Create a Python program with an enumeration: a set of named constants.

Instructions

  • Import Enum from enum and define the class State with three members: PENDING = 1, IN_PROGRESS = 2 and DONE = 3.
  • Read an integer, the state code, and look up the member with State(code). If it does not exist, Python raises ValueError.
  • If it exists, show State: NAME (VALUE), using .name and .value. Otherwise, show Unknown state.
Input
2
Output
State: IN_PROGRESS (2)

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise