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 Introducing Lists Build an Application Display the List

Ben McMahan
Ben McMahan
7,921 Points

Here's my solution

Added a few things, such as checking for the command inputs after they had been converted to uppercase and a friendlier show_list()

# Create a new empty list called shopping_list
shopping_list = []


def add_to_list(item):
    """
    Add To List
    Adds a new item to the shopping list
    :param item: Item to be added
    :return: 
    """
    # Add the item to the list
    shopping_list.append(item)
    # Notify the user that the item was added and state the number of items in the list currently
    print("Added! List has {} {}".format(len(shopping_list), "items" if len(shopping_list) > 1 else "item"))


def show_help():
    """
    Show Help
    Shows the menu of available commands
    :return: 
    """
    print("What should we pick up at the store?")
    # Multiline print statement
    print("""
    Enter 'DONE' to stop adding items.
    Enter 'SHOW' to show the items on the list. 
    Enter 'HELP' for this help.
    """)


def show_list():
    """
    Show List
    Prints the shopping list to the screen
    :return: 
    """
    shopping_list_length = len(shopping_list)
    if shopping_list_length == 0:
        print("There are no items on the shopping list. Add one!")
    else:
        are_is = "are" if shopping_list_length > 1 else "is"
        item_items = "items" if shopping_list_length > 1 else "item"
        print("There {} {} {} on the shopping list:".format(are_is, shopping_list_length, item_items))
        for item in shopping_list:
            print(item)


show_help()

# Infinite Loop
while True:
    new_item = input("> ")

    if new_item.upper() == "DONE":
        break
    elif new_item.upper() == 'HELP':
        show_help()
        continue
    elif new_item.upper() == 'SHOW':
        show_list()
        continue

    # Call add_to_list with a new item as an argument
    add_to_list(new_item)

show_list()

1 Answer