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.

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)

Edited 1 Year Ago by sneekula

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

This article has been dead for over six months. Start a new discussion instead.