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 trialAmulya Arora
6,133 PointsGetting 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
19,870 PointsPlease post the properly formatted code causing the error, check the markdown cheetsheet below 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
6,133 PointsAmulya Arora
6,133 PointsThanks a lot. The code is working perfectly in pycharm.