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 Introducing Lists Build an Application Display the List

andrew neves
andrew neves
3,055 Points

I updated my Help function but i keep receiving a NameError for "add_to_list" not being defined. but it's defined twice

shopping_list = []

def show_help():
  print("What should we pick up at the store?")
  print(""" 
  Enter 'DONE' to stop adding iems.
  Enter 'HELP' for this help.
  Enter 'SHOW to see your current list.
  """)


  def add_to_list(item):
    shopping_list.append(item)
    print("Added! List has {} items.".format(len(shopping_list)))

  #Define a function name show_list that prints all the items in the list
  def show_list():
    print("Heres your list:")
    for item in shopping_list:
      print(item)

show_help()
while True:
  new_item = input("> ")
  if new_item == 'DONE':
     break
  elif new_item == 'HELP':
      show_help() 

  elif new_item == 'SHOW':
      show_list()
      continue


  add_to_list(new_item)



show_list()

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The functions add_to_list and show_help are indented. This makes them both local functions only accessible within the show_help function.

Reducing the indentation should fixed this issue.

Post back if you need more help. Good luck!!!

andrew neves
andrew neves
3,055 Points

Thank you for the help! ended up fixing that but now im getting a name error saying my show_list() function isnt being defined. Not sure if this has to do with indentation aswell. Any ideas ?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Please confirm the line “def show_list():” has no indentation. If it does, it would cause the same problem as before where the function becomes local to the previously defined function.

andrew neves
andrew neves
3,055 Points

Got it ! Thanks for the help agai. Greatly appreciated.🙏🏼

I know this post was from ages ago lol but if you're still here of people are peaking as well at your question.. you did as well forgot to put continue on your elif for help. Anyways, aside from that you're a super panda!

Michael Ford
Michael Ford
3,432 Points

Indentation is easy to overlook!

Got me a couple of times =)

I use the Visual Code IDE to write my code, it highlights syntax errors and potential issues while I am working.

Check it out here: https://code.visualstudio.com/download