Get Enum Member by user input in Python

Viewed 102

I want to take ENUM member from user input. Till now I have found where I have to do this as hard code or y using the ENUM member value.

Example code which works
code reference

from enum import Enum

class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4


seas = Season.SPRING
print(seas)

Example code which I want to execute

from enum import Enum

class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4

print("Give the User Input\n")
x = input() # user will write SPRING
seas_x = Season.x
print(seas_x)

I know the former one will not work as I am passing a string type variable to Season ENUM which has no member of that type/name. But is there any workaround to achieve it? For some restrictions, I have to use the ENUM member name as user input.

0 Answers
Related