2

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.

3
  • 2
    You could use Season[x]. Commented Mar 24, 2021 at 11:19
  • 2
    Does this answer your question? Convert string to Enum in Python Commented Mar 24, 2021 at 11:29
  • @Heike thanks. It is working. Though it is still throwing me an Error which is another issue regarding pybind11. @FlorianGD yes. I have mistakenly submit NO in your suggestion. Commented Mar 24, 2021 at 12:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.