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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsProblem 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
41,120 PointsYour 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)?
Stuart Wright
41,120 PointsThe 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
Treehouse Moderator 91,253 PointsAppreciate that, thanks Steven. I'll let you know if it works. :)
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsI'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.