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

My Python shopping_list throws a NameError, despite holding to the exact design of the tutorial.

Here is my code, in a file called shopping_list.py:

shopping_list = []

print("Enter 'Done' to stop adding items.")

while True: new_item = input("> ")

  shopping_list.append(new_item)

  if new_item == "DONE":
      break
  print("Here's your list.")

for item in shopping_list: print(item)

And here is the error when I enter the first item, "box" for instance:

dyorloh@trademptra:~$ python shopping_list.py Enter 'Done' to stop adding items.

box Traceback (most recent call last): File "shopping_list.py", line 6, in <module> new_item = input("> ") File "<string>", line 1, in <module> NameError: name 'box' is not defined

4 Answers

To ds1 -- I figured it out. When I type python shopping_list.py into my command line, the machine executes in Python 2. When I type python3 shopping_list.py, it executes fine.

Great! Yeah, I type "python shopping_list.py" in the Windows command prompt but don't think I have Python2 installed... just Python3. That's fantastic you figured it out... now you're good to go!

Hi, Dan

Not sure why you're getting a name error if you're running this per the normal way in the course. (running the entire script in the python console of Workspaces). Python should take whatever you type in the input (ex. box) and interpret as a string and not a variable. See if this code works for you... it ran fine in my editor

shopping_list = []
print("Enter 'done' to stop adding items.")
while True:
    new_item = input("> ")
    if new_item.lower() == 'done':  # used lower() method so case of input doesn't matter (ex. "DONE" or "done" will work)
        break
    shopping_list.append(new_item)
    print("Here's your list:")
    for item in shopping_list:      # printing each item of the list out
        print(item)

I've checked your code line by line with mine. They match up. I'm writing my code into a file called shopping_list.py using vim, then calling the file in my shell with $ python shopping_list.py It still throws that same error, not recognizing the first item.

Huh... I honestly don't know right off. Sorry about that. You could ask a Treehouse moderator or someone that's been python-ing longer. Have you tried running it from your IDE (ex. Vim) instead of the shell? My code worked fine when I ran it from PyCharm before posting it to you. I've never used Vim, but I'm sure you could step through your code (like in debugging mode) and see it execute line by line which could provide some insight. I guess you could also try entering 'box' vs. just box to make sure it knew it was a string (but really don't know why you would need to... input() should kick everything back as a string). Sorry I couldn't be more helpful! D.

I appreciate the effort and advice. I'm more interested in getting it to work on my computer (or any computer) than whether it runs here in the tutorials.

Understood. There's something (probably small) that we're missing as I just ran the same script on my Windows Command prompt and the IDLE's python shell. Frustrating. Anyway, I'd just ask someone directly like a moderator (typing "@name")