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 trialAaron Brooks
356 PointsNameError: name is not defined
Here is my code for the shopping list. It is almost identical to the video:
shopping_list = list()
print("What would you like to add to the list") print("Enter DONE what finished")
while True: new_item = input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item)
print ("Your list contains {}".format(len(shopping_list)))
continue
for items in shopping_list: print(items)
When I run it, whatever I add as my first items gives me the following error:
What would you like to add to the list Enter DONE what finished
eggs Traceback (most recent call last): File "shoppinglist.py", line 7, in <module> new_items = input("> ") File "<string>", line 1, in <module> NameError: name 'eggs' is not defined
Any help would be great
4 Answers
Szabolcs Komjathi
4,706 PointsHi there again, what's the version of your python? (type 'python -V' to find out ) here's a modified code, optimized for Python 2.6.6.
shopping_list = list()
print("What should we pickup at the store? ")
print("Enter DONE to stop adding items. ")
while True:
# use raw_input instead of input
new_item = raw_input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item)
# use {0} instead of {} when formatting
print("Added! List has {0} items.".format(len(shopping_list)))
continue
print("Here's your list:")
for item in shopping_list:
print(item)
Reference: https://docs.python.org/2/library/functions.html#raw_input https://docs.python.org/2/library/string.html#formatstrings
HTH
Szabolcs Komjathi
4,706 PointsDear Aaron, i had the same problem, check out the spacing and indentation within the while loop and the if statement. Each block of code is recognized by the same indentation. It's very important.
Here's my shopping_list.py. Check it out!
shopping_list = list()
print("What should we pivkup at the store? ")
print("Enter DONE to stop adding items. ")
# while loop block
while True:
# read input from console, infinitely
new_item = input("> ")
# until you type "DONE", then loop will be stopped
if new_item == 'DONE':
break
# if you type anything else then "DONE", the input will be added to your shopping list
shopping_list.append(new_item)
print("Added! List has {} items.".format(len(shopping_list)))
# continue will result a jump, back to the beginning of while loop (new input will be asked on the console)
continue
# end of while loop block
# new block of code, only run if the loop is over (for eg: the input was "DONE")
print("Here's your list:")
# echo the list items
for item in shopping_list:
print(item)
#end on block
HTH
Aaron Brooks
356 PointsThanks for the response! Unfortunately I am getting the same error when copying your code. It could possibly be my version of python as I am not working in the workspace provided on here.
Aaron Brooks
356 PointsFYI for anyone with this problem. I had both python 2 and 3 installed so i needed to call pythong 3 instead of 2 to have the code working as originally written
Kenneth Love
Treehouse Guest TeacherYeah, using Python 2, input()
does an eval
on the values passed in, which means it's effectively running whatever is passed in as Python code (scary), so it'll look for variables, etc. Python 3 fixed this horrible loop hole.
This is just one of the reasons we using Python 3 instead of Python 2.
Aaron Brooks
356 PointsAaron Brooks
356 PointsHello,
I am running Python 2.7.5+. Your code works. I will compare it with mine to help me understand why mine did not work. Thank you so much!
Aaron Brooks
356 PointsAaron Brooks
356 Pointschanging "input()" to "raw_input()" did the trick! Thanks!