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

Servicios Adquiridos SA de CV
Servicios Adquiridos SA de CV
886 Points

My list has a huge bug but I don't get it :(

Hello! I typing at the same time while I was following the instructions and I don't get why my code repeat the items in the list, and also, doesn't show some of the items that you want to add to the list and sometimes only shows a blank space.

import os

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



shopping_list = []


def add_to_list(item):
    show_list()
    if len(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)

        show_list()


    shopping_list.append(item)
    print("Added! List has {} items.".format(len(shopping_list)))
    show_list()



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 list.
""")


def show_list():
    clear_screen()


    print("This is on your bag:")

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

    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)


    add_to_list(new_item)
Steven Parker
Steven Parker
230,274 Points

Good job on formatting your code! :+1: A link to the course page you are working with would also be helpful.

And can you give an exact sequence of inputs that will demonstrate the issue(s)?

1 Answer

Steven Parker
Steven Parker
230,274 Points

At first glance, I notice that in "add_item", there's a call to "show_list" right at the beginning, another one that might run (if the list is empty) in the middle, and yet another one at the end. That might account for the repeats.

I'll take a closer look when you post your test inputs.