Python Python Basics (2015) Shopping List App Shopping List Introduction

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 82,531 Points

Problem with the input function

I have a script for the first shopping list program and I've managed to get it working with Workspaces however on my local system I keep getting a name error.

# shopping_list.py  From Treehouse

# make a list to hold onto our items
# print out instructions on how to use the app
# add new items to our list
# be able to quit the app
# print out the list

#SCRIPT

# make a list to hold onto our items
shopping_list = []

# print out instructions on how to use the app
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")

while True:
    # ask for new items
    new_item = input("> ")

    # be able to quit the app
    if new_item == 'DONE':
        break

    # add new items to our list
    shopping_list.append(new_item)

# print out the list
print("Shopping List includes: ")    

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

Was the input function recently added or changed?

I notice workspaces uses python Python 3.5.0 but my version is Python 2.7.13 Although the Python shell that I have uses 3.6.4. It's all very odd.

2 Answers

Stuart Wright
Stuart Wright
41,068 Points

Your script works correctly for me on my machine, which is running 3.5.2.

I know that in Python 2.x the input function used to be called raw_input, so I'm guessing that you are running your script using Python 2.x rather than 3.x.

So one solution would be editing the name of that function accordingly, but I think it would be better to try to work out how to get your script running on Python 3.x. What operating system are you on and how are you running your script (using an IDE, or just running python shopping_list.py from console)?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 82,531 Points

I'm using Windows 10, Windows Command Prompt to run python. I also have my Git Bash command line but it doesn't run Python properly.

It sounds like I just need to work out how to update Python then because on Windows Command prompt, as I say it shows up as Python version 2.

Stuart Wright
Stuart Wright
41,068 Points

The best thing to do will be to work out where about your Python 3 executable is stored (it'll be called python.exe, so you can search your system for that). Sounds like it'll find at least two - one for Python 2 and one for Python 3. If you open a command prompt from within the folder that contains your Python 3 executable, you can then run "python.exe shopping_list.py" to check that it works.

Once you confirm that this works, you should add this folder to your environment variables, so that you can simply type python from a command prompt no matter which folder you're in, and it'll know you mean the python.exe file for your Python 3 installation.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 82,531 Points

Appreciate that, thanks Steven. I'll let you know if it works. :)