Python Forum
NameError! shopping_list not defined
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError! shopping_list not defined
#1
def show_help():
   # print out instructions on how to use the app
   print("What should we pick up at the store?")
   print("""
Enter 'DONE' to stop adding items.
Enter 'HELP' for this help.
Enter 'SHOW' to see your current list.
""")

def show_list(shopping_list):
   # print out the list
   print("Here's your list:")

   for item in shopping_list:
       print(item)

def shop():
   shopping_list = []
   while True:
       # ask for new items
       new_item = input("> ")

       # be able to quit the app
       if new_item == 'DONE':
           break
       elif new_item == 'HELP':
           show_help()
           continue
       elif new_item == 'SHOW':
           show_list(shopping_list)
           continue
       add_to_list(shopping_list, new_item)

def add_to_list(shopping_list, new_item):
   # add new items to our list
   shopping_list.append(new_item)
   print("Added {}. List now has {} items.".format(new_item, len(shopping_list)))
   return shopping_list


#define new main function
def main():
   #ident correctly given new main function definition
   show_help()
   shop()
   show_list(shopping_list)

main()
Moderator snippsat: Added code tag look at BBcode help
and Smart Questions.
Reply
#2
The error is quite clear - your dont have shopping_list defined when you call show_list(shopping_list) on line 46. Perhaps you should add return statetement with shopping_list into your shop() function and use
shopping_list = shop()
show_list(shopping_list)
in your main function?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 312 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,122 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,913 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,353 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,522 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,935 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,374 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 15,032 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,404 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,902 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020