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.

user10634362
  • 549
  • 5
  • 21
  • 2
    You could use `Season[x]`. – Heike Mar 24 '21 at 11:19
  • 2
    Does this answer your question? [Convert string to Enum in Python](https://stackoverflow.com/questions/41407414/convert-string-to-enum-in-python) – FlorianGD Mar 24 '21 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. – user10634362 Mar 24 '21 at 12:08

0 Answers0