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 trialjamie Macdiarmid
2,048 Pointsstuck
add new items to our list
shopping_list.append(new_items)
When I try to run my code this appears. I've double checked but cant see what's wrong.
Traceback (most recent call last):
File "shopping_list.py", line 17, in <module>
shopping_list.append(new_items)
NameError: name 'new_items' is not defined
Can someone help?
jamie Macdiarmid
2,048 PointsI've followed Kenneth's video to the letter and his works. How do I define the new items again?
Afrid Mondal
6,255 PointsCan you paste the code. It would be really helpful.
Haider Ali
Python Development Techdegree Graduate 24,728 PointsHi Jamie. As Afrid said, please paste all of your code so we can help you with your problem.
jamie Macdiarmid
2,048 Points#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 item
new_item = input("> ")
# be able to quit the app
if new_item == 'DONE':
break
# add new items to our list
shopping_list.append(new_items)
#Print out the list
print("Here's your list:")
for item in shopping_list:
print(item)
DOES THIS HELP?
[MOD: added ```python markdown formatting -cf]
4 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsIn your while
loop, you define new_item
but not "new_items":
while True:
# ask for new item
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) # <-- changed to new_item
jamie Macdiarmid
2,048 PointsOh. Thanks Chris
Tobias Helmrich
31,603 PointsHey Jamie,
I think the problem is that you call the variable new_item but you're appending the variable new_item*s* to the shopping_list array.
I hope that helps!
jamie Macdiarmid
2,048 PointsOh. Ok. Thanks Tobias.
Niko Questera
701 PointsI also got a similar error...but I made sure there is not typo in the code. Can anyone help?
Nikos-MacBook-Air:PythonLearning nikoquestera$ python treehouse_test.py
What should we pick up at the store
Enter DONE to stop adding items
> apples
Traceback (most recent call last):
File "treehouse_test.py", line 13, in <module>
new_item = input("> ")
File "<string>", line 1, in <module>
NameError: name 'apples' is not defined
Nikos-MacBook-Air:PythonLearning nikoquestera$
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")
# ask for new items
# be able to quit the app
# add new items to our list
# print out the list
while True:
new_item = input("> ")
if new_item == "DONE":
break
shopping_list.append(new_item)
print("Here is your list: ")
for item in shopping_list:
print(item)
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,454 PointsAre you running Python 2? Check using $ python --version
In Py2, input is evaluated which is why when you type apples it raises an error that the variable apples
is not defined. To pass in Py2 strings must be quoted: "apples".
Otherwise, run Python 3.
Niko Questera
701 Points2.7.10 Thank you Chris for prompt answer :)
Afrid Mondal
6,255 PointsAfrid Mondal
6,255 PointsHey Jamie, make sure you defined that "new_items" first, then try to use it.