Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

https://teamtreehouse.com/workspaces/35526412


shopping_list = []

def add(): shopping_list.append(item)

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

main()


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)))

show_help()

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()

show_list()