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

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Shopping list code problem:

Hello folks,

I trying to code a shopping list programm by myself and have a little problem.

My code is:

shopping_list = []

print ("Type in your items and STOP if you want to quit")

list_item = input("> ")

while True:
    if list_item == 'STOP':
        print("See you later")
        break

    shopping_list.append(list_item)

print("Your shopping list:")

for x in shopping_list:
    print(x)

I can see the prompt and can type an item but if I press enter I see this error:

Type in your items and STOP if you want to quit
> cool
Traceback (most recent call last):
  File "test.py", line 5, in <module>
    list_item = input("> ")
  File "<string>", line 1, in <module>
NameError: name 'cool' is not defined

Why is my item not defined? Can someone help me ?

Grigorij

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Carlos,

thank you for your reply :)

I figured out that I am using python 2 in my console and not python 3.

So I have changed input to raw_input and the error disappeared.

Thank you again

Grigorij

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

I would recommend you to put the:

list_item = input("> ")

Inside of the:

while True:
    if list_item == 'STOP':
        print("See you later")
        break

Because if you don't put "STOP" in the first place you are never going to be able to get out of that while loop. Besides of that I can't see why is throwing at you that "NameError".

I hope that helps a little bit.