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

Last add_to_list not workinh

Here is my code snapshot https://w.trhou.se/m6d49sgdlc

I'm getting an error "> ban
Traceback (most recent call last):
File "shopping_list.py", line 36, in <module>
add_to_list()
NameError: name 'add_to_list' is not defined
" My code seems to be exact as Craig's What am I missing?

Thanks, Scott

2 Answers

Steven Parker
Steven Parker
243,160 Points

Because of the indentation level, both "add_to_list" and "show_list" are created as internal functions of "show_help", but they should be independent so they can be accessed from the main program.

And on line 19, there is a reference to "shoppping_list" (with 3 "p"s) instead of "shopping_list".

Also, the call to "add_to_list" on line 36 has no argument, but the function is defined as needing one.

Thanks. I just watched the video again and re wrote my code, it seems exactly the same as the video but I'm still getting the same error. Any examples you could point me to? Thanks

Steven Parker
Steven Parker
243,160 Points

You haven't provided a new snapshot, but here's an indent example from the previous one:

def show_help():
  print("What should I buy?")
  print("""
  ENTER 'DONE' to stop adding ites.
  ENTER 'HELP' for this help.
  ENTER 'SHOW' to see your current list.
  """)

  def add_to_list(item):  # <-- notice this line (12) is indented, but should not be
    shopping_list.append(item)
    print("Added! List has {} items.".format(len(shopping_list)))

Thanks, Steve! I just got it, it was only an indentation issue! :)