- MianQi2019
- Posts: 7
- Joined: Tue Oct 21, 2025 3:14 am
NameError: name 'Color' is not defined
Anyone has tested these code on https://docs.python.org/3/tutorial/controlflow.html ?
if there were no "Tab" before "color...", it would prompt:
if one "Tab" added, it would prompt:
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 :(")
Code: Select all
File "<stdin>", line 5
color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
^^^^^
SyntaxError: invalid syntax
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
Re: NameError: name 'Color' is not defined
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
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)
Re: NameError: name 'Color' is not defined
Works for me with 3.11.2
This is the same OP who previously had a problem through running Python 3.5.3 ...
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
Code: Select all
File "<stdin>", line 5
color = Color(input("Enter your choice of 'red', 'blue' or 'green': "))
^^^^^
SyntaxError: invalid syntax
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
ghp wrote: ↑Mon Nov 10, 2025 8:33 am
Check your python version with "python3 --version" orCode: 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
Now I run Python3 on Ubuntu 24.04 other than Raspberry 3B+.This is the same OP who previously had a problem through running Python 3.5.3 ...
Re: NameError: name 'Color' is not defined
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.
Re: NameError: name 'Color' is not defined
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.there must be something in your source file which causes the issue
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
I typed in the code carefully this time, now it works. Thanks. I'm newbie for python, sorry for these disturbances.
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion