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 Collections (Retired) Lists Redux Shopping List Take Three

Karol Zientek
Karol Zientek
2,006 Points

Shopping list 3

Hi!

I have got a question concerning the Python video about Shopping LIst 3. I don't really see the point of putting: "count += 1" and "spot += 1" in the line 13 and 40. I mean, I don't understand why it is that. Could anyone explain me why?

shopping_list = []


def show_help():
    print("\nSeparate each item with a comma.")
    print("Type DONE to quit, SHOW to see your current list and HELP yo see this message.")


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

print("Can you provide me a list with items you want to buy, please?")
show_help()

while True:
    new_stuff = input("> ")

    if new_stuff == "DONE".lower():
        print("\nHere's your list:")
        show_list()
        break
    elif new_stuff == "HELP".lower():
        show_help()
        continue
    elif new_stuff == "SHOW".lower():
        show_list()
        continue
    else:
        new_list = new_stuff.split(",")
        index = input("Add this at the certain spot? Press enter for the end of the list,"
                      "or give me a number. Currently items in the list.".format(
                len(shopping_list)))
        if index:
            spot = int(index) - 1
            for item in new_list:
                shopping_list.insert(spot, item.strip())
                spot += 1
        else:
            for item in new_list:
                shopping_list.append(item.strip())

I would really appreciate if there is anyone who knows the answer.

2 Answers

Ramiro Martinez
Ramiro Martinez
13,858 Points

With count += 1 you're incrementing the number's item in your list (for display the number with the print function), every time you put a new item. And spot += 1, is for when you put more than 1 item in the list, separated by ",", the for loop will put every item in the list, incrementing its index number by 1.

count += 1 is equivalent to count = count + 1. It's just used to keep track the index of the item being printed to the console.

Karol Zientek
Karol Zientek
2,006 Points

Can you explain it in more "human" way of thinking? I think I still don't get it. Can you do it, please?