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 trialCharles Harpke
33,986 PointsSHOW doesn't display list...
When I type 'SHOW' to display the list(from the console) I get the following message: "Traceback (most recent call last): File shopping_list_3.py, line 29 in module show_list() NameError: name 'show_list is not defined'"
Here is the code:
shopping_list = []
def show_help():
print("\nSeperate each item with a comma.")
print("Type DONE to quit, SHOW to see the current list, and HELP to get this message.")
def show_list():
count = 1
for item in shopping_list:
print("{}: {}".format(count, item))
count +=1
print("Give me a list of things you want to shop for.")
show_help()
while True:
new_stuff = input("> ")
if new_stuff == "DONE":
print("\nHere's your list:")
show_list()
break
elif new_stuff == "HELP":
show_help()
continue
elif new_stuff == "SHOW":
show_list()
continue
else:
new_list = new_stuff.split(",")
index = input("Add this at a certain spot? Press enter for the end of the list, "
"Or give me a number. Currently {} items in the list.".format(len(shopping_list)))
if index:
spot = int(index) - 1
for item in new_list:
shopping_list.insert(spot, item.strip())
spot += 1
else:
for item in new_list:
shopping_list.append(item.strip())
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Charles,
I fixed your code formatting for you. You were on the right track but only used 2 backticks instead of 3.
I think that your main program can't find show_list()
because it looks like you may have indented it one level when you defined it. This would place it within the show_help()
function and it would not be available outside that function.
Kenneth Love
Treehouse Guest TeacherYep, your show_list()
belongs to show_help()
(nested functions are actually a really neat part of Python but we're no where near there yet) so you can't call it directly.
Good catch, Jason Anello!
Charles Harpke
33,986 PointsCharles Harpke
33,986 PointsThanks for fixing the code...that works....Thank you!