Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Logic in Python The Exception to the Rule

Soumik Rakshit
Soumik Rakshit
2,745 Points

Shouldn't the exception should be NameError instead of ValueError?

I passed the exception ValueError, but i got the error on my Linux terminal called NameError. So I passed the exception NameError and now the program is working smoothly.

1 Answer

28,556 Points

Treehouse uses and teaches Python 3. In that version of Python the code demonstrated results in a ValueError. When ran on Python 2 on the other hand it does indeed result in a NameError.

Python 3 and Python 2 have some fundamental changes between them so code designed for Python 3 is not guaranteed to perform correctly in Python 2 and vice versa. I assume you have Python 2 installed on your Linux machine. If you installed Python 3 and ran the code there, you should get the same behavior as demonstrated in the video.

I'm running Linux as well. I have both python versions 2.7(default) and 3.5 loaded on my system. I think it's common to have both version installed.

Since the default for my system is python 2.7, if I run

python exception.py

I will get a NameError. But if I run:

python3 exception.py

I will get the ValueError. So, the code must be changed to coordinate with the version you run at the command prompt: use VaueError when running python3 but use NameError if you are running "python" at the command prompt (if version 2.7 is your system default.)

On Linux you can check your system default with python --version