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

Jordan Bester
Jordan Bester
4,752 Points

what is wrong with my python script? Shopping list not working in shell?... says count += 1 invalid syntax

1 Answer

Chase Frankenfeld
Chase Frankenfeld
6,137 Points

Hey mate. This is my first attempt at helping anyone. Mostly there were simple errors that such as spelling errors, incorrect spacings (make sure you set your workspace to 4 spaces), and forgetting brackets in places. I added comments where I changed stuff. Hopefully it helps.

 shopping_list = []


def show_help():
    print("\nSeperate each item with a comma.")
    print("Type DONE to quit, SHOW to see the current list, HELP to get the help message.")


def show_list():
    count = 1 
    for item in shopping_list:
        print("{}: {}.".format(count, item))  # This had a double bracket after .format((
        count += 1

print("Give me a list of things you want to shop for.")
show_help()


while True:
    # CORRECTIONS - Make sure that spacing is correct. 4 spaces - most were only 2 spaces or 1 etc
    new_stuff = input(">")

    if new_stuff == "DONE":
        print("\nHere's your list!")
        show_list()
        break
    elif new_stuff == "HELP":
        show_help()
        continue
    elif new_stuff == "SHOW":
        show_list()  # Wrote as show_list not show_list()
        continue
    else:
        new_list = new_stuff.split(",")
        index = input("Add this at aH 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:
            # Need to make sure this was inside the loop
            shopping_list.insert(spot, item.strip())  # had : instead of a closing brackets
            spot += 1
    else:
        for item in new_list:
            # Needed to make sure this was inside the loop to
            shopping_list.append(item.strip())  #spelling error --> changed sjhopping_list to shopping_list

[MOD: added 'python' to the markdown block code for colorization. -- cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Hey Chase Frankenfeld! Thanks for helping out in the forum!

I moved your comment to an Answer and tweaked the formatting a bit. Hope to see you answering more posts!