Python 2 Enum Example

I found this code which is supposed to illustrate how enums work. I'm aware things are differnt now in Python 3, but I want to make sense of this example. What do I need to input to get print("You chose the easy option") to execute please?

I've tried 1, Easy and Choice.Easy so far with no success.

def enum(**enums):
    return type('Enum', (), enums)

Choice = enum(Easy = 1, Medium = 2, Hard = 3)
choice = input("Enter choice: ")

if choice == Choice.Easy:
    print("You chose the easy option")
elif choice == Choice.Medium:
    print("You chose the medium option")
elif choice == Choice.Hard:
    print("You chose the hard option")
else: 
    print("You should choose one of the three levels!")

enum — Support for enumerations — Python 3.8.5 documentation, SQUARE: 2>). The __members__ attribute can be used for detailed programmatic access to the enumeration members. For example, finding all the aliases: >>> That example does not create an enum, just a class with some members that have integer values. The reason it is not working in Python 3 is because in Python 2 input() evaled whatever was entered, so typing 1 actually returned an int; in Python 3 input became the same as Python 2's raw_input(), which returns strs and makes you do any necessary conversions.

The input which user has entered is string type and in Choice it has integer. Make change:

Choice = enum(Easy = '1', Medium = '2', Hard = '3')

or

choice = int(input("Enter choice: "))

in this case you need to handle exception valueError

8.13. enum — Support for enumerations — Python 3.4.10 , 2. Creating an Enum�. Enumerations are created using the class syntax, which makes them easy to read and write. An alternative creation method is� Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over.

This is not working because you are comparing string with integer. you can take input as integer.

choice = int(input("Enter choice: "))

How can I represent an 'Enum' in Python?, For example, enum members are distinct from integers, and they are composed of a name and a value . class Animal(Enum): DOG = 1 CAT = 2 print(Animal. The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (__str__(), __add__(), etc.), descriptors (methods are also descriptors), and variable names listed in _ignore_.

enum � PyPI, This package provides a module for robust enumerations in Python. An enumeration object is created with a sequence of string arguments to the Enum() � Enumerations in Python are implemented by using the module named “enum“.Enumerations are created using classes. Enums have names and values associated with them. Properties of enum: 1. Enums can be displayed as string or repr. 2. Enums can be checked for their types using type(). 3. “name” keyword is used to display the name of the enum

enum in Python, import enum. # creating enumerations using class. class Animal(enum.Enum):. dog = 1. cat = 2. lion = 3. # printing enum member as string. python documentation: Creating an enum (Python 2.4 through 3.3) Example. Enums have been backported from Python 3.4 to Python 2.4 through Python 3.3.

Python enum tutorial - working with enumerations in Python, Python enum simple example II. The next example presents some other basic functionality of a Python enum. simple2.py. #!/usr/bin/python3 from� Python Enum. Python Enum is a set of symbolic names. The number of members of an Enum class are bound to be unique and constant values. In this tutorial, we shall learn how to create an enum in your Python program, how to define the constants in it, access these constants and the datatypes we can assign.

Comments
  • Python 2 will no longer be supported in less than one month. Do not use it.