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 Collections (2016, retired 2019) Lists Shopping List Take Three

shopping_list_3 , Program is not working properly. After the first input list does not update.

import os

shopping_list = []

def clear_screen(): os.system("cls" if os.name == "nt" else "clear")

def show_help(): clear_screen() 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 add_to_list(item): show_list() if shopping_list: position = input("Where should I add {}?\n" "Press ENTER to add to the end of the list\n" "> ".format(item))

else:
    position = 0

    try:
        position = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(new_item)

    show_list()
return position

def show_list(): clear_screen()

print("Here's your list:")

index = 1
for item in shopping_list:
    print("{}. {}".format(index, item))
    index += 1

    print("-" * 10)

show_help()

while True: new_item = input("> ")

if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
    break
elif new_item.upper() == 'HELP':
    show_help()
    continue
elif new_item.upper == 'SHOW':
    show_list()
    continue
else:
    add_to_list(new_item)

show_list()

3 Answers

Paul Ehizuelen Jr.
Paul Ehizuelen Jr.
4,379 Points

Not sure how this will appear on your screen, but first i'd like to kindly point out that the way you uploaded your code made it a little difficult to troubleshoot. I think the markdown cheatsheet should help for any future questions. If you are use an IDE always convert to spaces before you copy and paste. The original question of the first list input not updating is actually not the issue, it is being added to the shopping list you just can't see it b/c this block below is a little off. In general you want to be careful with your indentation, that might be why your code wasn't printing the list and the "- *10". Below is your code as I understood it.

def add_to_list(item):
    show_list()
    if shopping_list:
        position = input("Where should I add {}?\n" "Press ENTER to add to the end of the list\n" "> ".format(item))

    else:
        position = 0

    try:
        position = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(new_item)
        print(shopping_list)
    show_list()
    return position

def show_list(): 
    clear_screen()
    print("Here's your list:")

index = 1
for item in shopping_list:
    print("{}. {}".format(index, item))
    index += 1

    print("-" * 10)
show_help()

Below is my complete edited version of your code. if any of the moves are unclear please let me know and I will be happy to clear it up (i went ahead and broke it up into two sections. The sections I touched and didn't touch. untouched

import os

shopping_list = []

def clear_screen():
    os.system("cls" if os.name == "nt" else "clear")

def show_help():
    clear_screen()
    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 """)

touched

def add_to_list(item):

    show_list()

    if shopping_list:
        position = input("Where should I add {}?\n" "Press ENTER to add to the end of the list\n" "> ".format(item))

    else:
        position = 0

    try:
        position = abs(int(position))
    except ValueError:
        position = None
    if position is not None:
        shopping_list.insert(position-1, item)
    else:
        shopping_list.append(new_item)
    clear_screen()
    show_list()

    return position

def show_list():
    clear_screen()
    print("Here's your list:")
    #print(shopping_list)
    index = 1
    for item in shopping_list:
        print("{}. {}".format(index, item))
        index += 1

    print("-" * 10)
    #print (index)

show_help()


while True:


    new_item = input("> ")

    if new_item.upper() == 'DONE' or new_item.upper() == 'QUIT':
        break
    elif new_item.upper() == 'HELP':
        show_help()
        continue
    elif new_item.upper == 'SHOW':
        show_list()
        continue
    else:
        add_to_list(new_item)


show_list()

Paul, thank you so much it worked. I copied the whole code into additional comments, after I submitted it , it looks like it broke the whole code into peaces. As far my indentation, I copied the code from my workspace to pycharm so can deffinatley , see the issue. Once again thank you so much, I spent hours and I could not figure it out.

Paul Ehizuelen Jr.
Paul Ehizuelen Jr.
4,379 Points

dude thank you! this was my first time answering a question and you got me feeling good about myself.

No problem , keep up the good work. Lol I will deffinatley need your asnwers.