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 Python Basics (2015) Shopping List App Shopping List Introduction

jamie Macdiarmid
jamie Macdiarmid
2,048 Points

stuck

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?

Afrid Mondal
Afrid Mondal
6,255 Points

Hey Jamie, make sure you defined that "new_items" first, then try to use it.

jamie Macdiarmid
jamie Macdiarmid
2,048 Points

I've followed Kenneth's video to the letter and his works. How do I define the new items again?

Afrid Mondal
Afrid Mondal
6,255 Points

Can you paste the code. It would be really helpful.

jamie Macdiarmid
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
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In 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
Tobias Helmrich
Tobias Helmrich
31,602 Points

Hey 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!

Niko Questera
Niko Questera
701 Points

I 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
Chris Freeman
Treehouse Moderator 68,423 Points

Are 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
Niko Questera
701 Points

2.7.10 Thank you Chris for prompt answer :)