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 Refactor

Jt Miller
Jt Miller
1,242 Points

What is it asking and whats does this teach ?.

I'm not really sure why its asking or what I'm supposed to do I tried moving everything from line 22 down and creating the function main but it didn't work and what is this supposed to teach.

This is what it asks:

shopping_list.py
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 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



def main():
     show_help()
    # make a list to hold onto our items
    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)


show_list(shopping_list)

1 Answer

Nick Gaudio
seal-mask
.a{fill-rule:evenodd;}techdegree
Nick Gaudio
Python Web Development Techdegree Student 2,133 Points

Hey Justice,

I took a look through your code and got it working on my computer. Everything is below...

1) First thing I noticed is that your script doesn't call the main function anywhere. You'll see that at the very bottom of the code I pasted. main()

2) Second thing I did was move your bottom line of "show_list(shopping_list)" (it's inside main which will be done running and dumped by the time you call show_list at the bottom (shopping_list) and will no longer be defined). You might want to move this line to just before your break underneath the elif of when you type in DONE. This will show the list and then end the program.

3) Instead of "return shopping_list" at the end of your add_to_list function, you might want to call show_list with shopping last as the argument. -- show_list(shopping_list). show_list at the end isn't totally necessary either. just the return of shopping_list isn't necessary since it won't be doing anything.

So to walk through the code...

we're now calling main at the very bottom of the script. This then kicks off your main function which takes us to your show_help function. After that finishes it comes back to main and defines shopping_list as an empty list. We then add the item with the input function and run it through the if and elif statements. If the new item doesn't equal "done", "help", or "show" then it calls add_to_list with shopping list and new item as your arguments. we then append the new_item to the shopping_list in the add_to_list function. After that's done you should be back at the prompt to add another item.

Hopefully this helped. I'm still pretty new, too.

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 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)))  
        ### optional - show_list(shopping_list)



def main():
    show_help()
    # make a list to hold onto our items
    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)


main()```