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 trialLuigi Santos
13,200 Points"Apple" is not defined
Copied the code from the video:
shopping_list = []
print("What should we pick up at the store?") print("Enter 'DONE' to stop adding items.")
while True: new_item = input("> ")
if new_item == 'DONE':
break
shopping_list.append(new_item)
for item in shopping_list: print(item)
When I input 'apple', the error message is:
Traceback (most recent call last): File "/Users/Luigi/Desktop/shopping_list.py", line 7, in <module> new_item = input("> ") File "<string>", line 1, in <module> NameError: name 'apple' is not defined
3 Answers
Frederik Andersen
10,012 PointsHi Luigi.
Are you using python on your computer or workspaces? For me it seems like you use Python 2.7.x instead of python 3.x.
You would get that error if you use python 2.7x and write "input" instead of "raw_input".
If this is the case i strongly recomend to update to python 3.x, because the python course on treehouse is in python 3..
The code for python 2.7x:
shopping_list = []
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = raw_input("> ")
if new_item == 'DONE':
for item in shopping_list:
print(item)
break
shopping_list.append(new_item)
The code for python 3.x:
shopping_list = []
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = input("> ")
if new_item == 'DONE':
for item in shopping_list:
print(item)
break
shopping_list.append(new_item)
Hope this helps :)
Frederik
Luigi Santos
13,200 PointsI figured it out, thanks to your help! It was the version of python. Switching it to 3.5 makes the code work. Thanks everyone!
Steven Parker
231,271 PointsCheck your code carefully for an indentation error. It looks OK otherwise.
Frederik Andersen
10,012 PointsHi Luigi.
I changed your code a little bit. Instead of printing the whole list each time you add another item, it prints the list when you enter 'DONE'.
I got a syntax error because of how the print was placed in the code, and the while loop was not intended. This code should work:
shopping_list = []
print("What should we pick up at the store?")
print("Enter 'DONE' to stop adding items.")
while True:
new_item = input("> ")
shopping_list.append(new_item)
if new_item == 'DONE':
for item in shopping_list:
print(item)
break
Hope this helps :)
Frederik
Luigi Santos
13,200 PointsHey Frederik.
That makes a lot of sense. Proper indentation definitely makes a big difference. I gave your code a try (copied and paste, while maintaining proper indentation). But I still received this error message:
What should we pick up at the store? Enter 'DONE' to stop adding items.
apples Traceback (most recent call last): File "/Users/Luigi/Desktop/shopping_list.py", line 8, in <module> new_item = input("> ") File "<string>", line 1, in <module> NameError: name 'apples' is not defined
Thank you for the help!
Luigi
Steven Parker
231,271 PointsSteven Parker
231,271 PointsTry blockquoting your code so the spacing shows up (in Python, indentation is everything).
Skip a line,
Then a line with just this: ```py note, those are accents, not apostrophes
Then your code,
Then a line with just this: ```