We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

8 posts • Page 1 of 1
MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

NameError: name 'Color' is not defined

Mon Nov 10, 2025 5:21 am

Anyone has tested these code on https://docs.python.org/3/tutorial/controlflow.html ?

Code: Select all

from enum import Enum
class Color(Enum):
 RED = 'red'
 GREEN = 'green'
 BLUE = 'blue'
color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
match color:
 case Color.RED:
 print("I see red!")
 case Color.GREEN:
 print("Grass is green")
 case Color.BLUE:
 print("I'm feeling the blues :(")
if there were no "Tab" before "color...", it would prompt:

Code: Select all

 File "<stdin>", line 5
 color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
 ^^^^^
SyntaxError: invalid syntax
if one "Tab" added, it would prompt:

Code: Select all

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "<stdin>", line 5, in Color
NameError: name 'Color' is not defined

ghp
Posts: 4448
Joined: Wed Jun 12, 2013 12:41 pm

Re: NameError: name 'Color' is not defined

Mon Nov 10, 2025 8:33 am

Can't reproduce this error message exactly in this line.

The "match" statement was introduced in python 3.10. With a 3.9-interpreter, I get a syntax error. With 3.13.3 interpreter, the code runs as expected.

Check your python version with "python3 --version" or

Code: Select all

import sys
print(sys.version)

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: NameError: name 'Color' is not defined

Mon Nov 10, 2025 1:58 pm

ghp wrote:
Mon Nov 10, 2025 8:33 am
The "match" statement was introduced in python 3.10. With a 3.9-interpreter, I get a syntax error. With 3.13.3 interpreter, the code runs as expected.
Works for me with 3.11.2

This is the same OP who previously had a problem through running Python 3.5.3 ...
MianQi2019 wrote:
Tue Oct 21, 2025 10:24 am
Python 3.5.3 (default, Nov 4 2021, 15:29:10)
Might be more of the same.

Or maybe not. With Python 3.5.3 I do get a Syntax Error but not where the OP gets theirs -

Code: Select all

pi@Pi4B:~/Python-3.5.3 $ ./python --version
Python 3.5.3
pi@Pi4B:~/Python-3.5.3 $ ./python /tmp/c.py
 File "/tmp/c.py", line 9
 match color:
 ^
SyntaxError: invalid syntax
That looks correct to me. There's no "match" keyword in 3.5.3 so it takes it as a variable name and is then expecting an "=" or a few other tokens, doesn't see any of those, so proclaims "invalid syntax".

Code: Select all

 File "<stdin>", line 5
 color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
 ^^^^^
SyntaxError: invalid syntax
That suggests to me the actual issue is in the lines before that though I don't know what, and the OP's post looks okay.

That there are multiple up-arrows under the token suggests to me the OP is now using something later than 3.5.3

MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

Re: NameError: name 'Color' is not defined

Wed Nov 12, 2025 11:52 am

ghp wrote:
Mon Nov 10, 2025 8:33 am

Check your python version with "python3 --version" or

Code: Select all

import sys
print(sys.version)

Code: Select all

>>> import sys
>>> print(sys.version)
3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]

MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

Re: NameError: name 'Color' is not defined

Wed Nov 12, 2025 11:59 am

This is the same OP who previously had a problem through running Python 3.5.3 ...
Now I run Python3 on Ubuntu 24.04 other than Raspberry 3B+.

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: NameError: name 'Color' is not defined

Wed Nov 12, 2025 12:51 pm

MianQi2019 wrote:
Wed Nov 12, 2025 11:52 am
3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
It works for me using 3.11.2 and for 'ghp' using 3.13.3 so there is no obvious reason it shouldn't work for you.

It would seem to me there must be something in your source file which causes the issue and what you have is not exactly the same as what you posted in the OP, what appears online, what 'ghp' and myself are using.

ghp
Posts: 4448
Joined: Wed Jun 12, 2013 12:41 pm

Re: NameError: name 'Color' is not defined

Wed Nov 12, 2025 5:05 pm

there must be something in your source file which causes the issue
If the problem still exists in your source file, then zip this file and post this zip archive in the forum here. Zip compression keeps possible 'strange characters', so this can be analyzed further.

Also possible to use "pylint" to check the code. See https://pypi.org/project/pylint/ for details.

MianQi2019
Posts: 7
Joined: Tue Oct 21, 2025 3:14 am

Re: NameError: name 'Color' is not defined

Thu Nov 13, 2025 1:05 am

hippy wrote:
Wed Nov 12, 2025 12:51 pm
MianQi2019 wrote:
Wed Nov 12, 2025 11:52 am
3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]
It would seem to me there must be something in your source file which causes the issue and what you have is not exactly the same as what you posted in the OP, what appears online, what 'ghp' and myself are using.
I typed in the code carefully this time, now it works. Thanks. I'm newbie for python, sorry for these disturbances.

8 posts • Page 1 of 1

Return to "Python"

AltStyle によって変換されたページ (->オリジナル) /