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! While you're at it, check out some resources Treehouse students have shared here.

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 (Retired) Shopping List Shopping List Project

Christopher Miller
Christopher Miller
614 Points

Python Shopping List Error on line 9

I've stepped thru the code line by line a few times, yet I continue to get the same error:

python shopping_list.py 
What should we pick up at the store?
Enter 'Done' to stop adding items. 
>apples 
Traceback (most recent call last): 
 File "shopping_list.py", line 9, in <module> 
  new_item = input("> ") 
 File "<string>", line 1, in <module>
NameError: name 'apples' is not defined 

Now I do want to let everyone know that this Python code runs fine on the Treehouse workspace.

However I'm also using A CentOS v7 x86_64 in a VM with the latest Virtual Box so I can play along while the video plays. The only difference is that the first line has the following:

#!/usr/bin/python2.7

or

#!/usr/bin/python

However it is pointing to line 9, so I would think there is some sort of error which is line 8, which is

while True:

Any ideas?

[MOD: updated formatting -cf]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The issue is related to the version of Python you are running.

in Python 2.7, the input is interpreted as a raw value, so entering apples is interpreted as a variable apples. Try entering 'apples' (with quotes).

In Python 3, the input is automatically cast as a string, so entering apples is interpreted as the string "apples"

Try running your script with python3 shopping_list.py, then you can omit the quotations marks.

Christopher Miller
Christopher Miller
614 Points

Sounds like the Treehouse Workspace is Python v3.

I will upgrade my version then.

thanks

Christopher Miller
Christopher Miller
614 Points

As a followup, incase others are reading.

I use Cent OS 7 as a VM and I added epel as a repository (IUS) and was able to install Python 3.4 (running both 2. and 3.4).

thanks

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

I am also using a VM. On my Windows 8.1 OS, I run VMware Player (free non-commercial) to host Ubuntu 14.04.

Once in Ubuntu, I use virtualenv -p /usr/bin/python2.7 venv2.7 or virtualenv -p /usr/bin/python3.4 venv3.4 to create a virtual environment with the specific version of Python I need. You can do this in the same code base directory. This allows you to run the code by simple switching the venv:

$ virtualenv -p /usr/bin/python2.7 venv2.7
$ virtualenv -p /usr/bin/python3.4 venv3.4
$ . ./venv2.7/bin/activate
(venv2.7)$ deactivate
$ . ./venv3.4/bin/activate
(venv3.4)$ deactivate
$

Note you'll have to pip install separately for each venv.