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 Python Basics (2015) Shopping List App Second Shopping List App

Sultana Naimi
Sultana Naimi
3,790 Points

When to use argument inside of functions?


shopping_list = []

def add(): shopping_list.append(item)

def main(): while True: item = input('> ') add() if item == 'done': break


Why do I need an argument in the 'add' function so this code works properly? The problem is it can't find the 'item' in the add function. But item has been defined in the 'main' function.

1 Answer

Sultana Naimi Hi Sultana, Here is my code, please post back if you are still unable to solve the code.

make a list to hold onto our items

shopping_items = []

print the instructions on how to use the app

def show_help(): print("Enter the items you want to pick up from the store!") print("Enter 'DONE' to quit the app") print("Enter 'SHOW' to check your list") print("Enter 'HELP' for help")

def show_list(): #print out the list print("Here is your list:") for item in shopping_items: print(item)

def add_item(): print("Adding {} to the list".format(new_item)) shopping_items.append(new_item) print("Added {} to the list. You now have {} in the list".format(new_item, len(shopping_items)))


while True: #ask for new items new_item = input("> ").lower() #be able to quit the app if new_item == 'done': break elif new_item == 'help': show_help() continue elif new_item == 'show': show_list() continue #add new items to our list add_item()