I am trying to get the following ENUM to work, but it's not working. Any help is appreciated.

class Colour(Enum):
    WHITE = (255,255,255)
    BLACK = (0,0,0)
    RED = (215,45,45)
    BLUE = (45,87,214)
    YELLOW = (230,223,48)
    GREEN = (45,194,64)

Using it: Colour.BLACK

Error:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "/Users/blooop/main.py"
Traceback (most recent call last):
  File "/Users/bloopppp/main.py", line 27, in <module>
    gameDisplay.fill(Colour.BLACK)
TypeError: invalid color argument

Process finished with exit code 1

I'm still learning Python, so all help is greatly appreciated!

Thank you!

You need to use at least Python version 3.4 ...

''' enum_test101.py
module enum is new in Python34
see: http://legacy.python.org/dev/peps/pep-0435/
'''

from enum import Enum

class Colour(Enum):
    WHITE = (255,255,255)
    BLACK = (0,0,0)
    RED = (215,45,45)
    BLUE = (45,87,214)
    YELLOW = (230,223,48)
    GREEN = (45,194,64)

print(Colour.BLACK.value)  # (0, 0, 0)

I installed it via $ pip install enum34. It was backported.
The syntax is registering perfectly with PyCharm v3 (by JetBrains).
See the answer here.

Thanks for the quick response.

In ubuntu there is a package python-enum34 for this, together with 3 other enum packages for python.

@Gribouillis I'm using a Mac. Not sure why it won't work though.

According to your traceback you are using Python2.7

Why not use just constants:

WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (215,45,45)
BLUE = (45,87,214)
YELLOW = (230,223,48)
GREEN = (45,194,64)

@sneekula — I would prefer Enums because Colour.GREEN etc. looks cleaner to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.