from enum import Enum
class Color(Enum):
RED = 0
GREEN = 1
BLUE = 2
match color:
case Color.RED:
print("I see red!")
case Color.GREEN:
print("Grass is green")
case Color.BLUE:
print("I'm feeling the blues :(")
However, the variable color is unknown, hence, NameError: name 'color' is not defined is shown. The solution is the declare a color variable and assign it any of the Color enum values, like so:
from enum import Enum
class Color(Enum):
RED = 0
GREEN = 1
BLUE = 2
color = Color.BLUE
match color:
case Color.RED:
print("I see red!")
case Color.GREEN:
print("Grass is green")
case Color.BLUE:
print("I'm feeling the blues :(")
Activity
pythongh-95986: Fix the example using match keyword
gh-95986: Fix the example using match keyword (#95989)
Merge branch 'main' into dictwatch
Merge branch 'main' into typewatch
pythongh-95986: Fix the example using match keyword (python#95989)
hauntsaninja commented on Oct 11, 2022
Looks like a PR was merged for this