Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Introducing Lists Build an Application Add Items

Amulya Arora
Amulya Arora
6,133 Points

Getting a syntax error in PYcharm

The code below when it is compiled in pycharm gives the error:

Traceback (most recent call last): File "ll.py", line 17, in <module> new_item = input("> ") File "<string>", line 1, in <module> NameError: name 'DONE' is not defined

but when it is run in the treehouse workspace it works without any errors

The code:

master_list = []

def add_to_list(item_to_be_added): master_list.append(item_to_be_added)

def show_help(): print("What should we pick up at the grocery store?") print("Enter 'DONE' to stop adding items") print("Enter 'HELP' for this help")

show_help() while True: new_item = input("> ")

if new_item == 'DONE':
    break
elif new_item == 'HELP':
    show_help()
    print()
    continue

add_to_list(new_item)

Can someone please tell me why this error is being reported?

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,822 Points

Please post the properly formatted code causing the error, check the markdown cheetsheet below :arrow_heading_down: for assistance.

I think you posted the working code, which works happy as can be in PyCharm. Your issue can be recreated by removing the single quotes around "'DONE'" on the if new_item... line. Generally when you get a "Name Error" xxx is undefined it is because it is looking for a variable name and you are thinking you are using a string. Missing the quotes is a common cause.

# Code works with single quotes - remove and test again, you get the same error message
master_list = []


def add_to_list(item_to_be_added):
    master_list.append(item_to_be_added)


def show_help():
    print("What should we pick up at the grocery store?")


print("Enter 'DONE' to stop adding items")
print("Enter 'HELP' for this help")

show_help()
while True:
    new_item = input("> ")
    if new_item == DONE: # missing quotes
        break
    elif new_item == 'HELP':
        show_help()
        print()
        continue

add_to_list(new_item)

# test and enter DONE and output is:
 if new_item == DONE:
NameError: name 'DONE' is not defined
Amulya Arora
Amulya Arora
6,133 Points

Thanks a lot. The code is working perfectly in pycharm.