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

In running shopping_list_3.py exercise, I receive error: insert() takes exactly 2 arguments(1 given)

My code isn't working exactly the same as Kenneth's in the video in Workspace:

The offensive line is apparently: shopping_list.insert(item.strip()) The error is: TypeError: insert() takes exactly two arguments(1 given)

Here is the code I copied from Kenneth in the video:

#N Bogaard --Shopping List 3 -- instructor Kenneth Love in Python Collections

shopping_list = []

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

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

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

while True:
    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()
        continue
    else:
        new_list = new_stuff.split(",")
        index = input("Add this at a certain spot? Press enter for the end of the list, "
                      "or give me a number. Currently {} items are in the list.".format(len(shopping_list)))
        if index:
          spot = int(index) - 1
          for item in new_list:
            shopping_list.insert(item.strip())
            spot += 1
        else:    
          for item in new_list:
            shopping_list.append(item.strip())

Did anyone else have a similar problem when using Workspace to compile?

(I am not running Python 3 on my laptop-- is there anything else I would need to change other than input to raw_input to run on Python 2 in my Windows Command Prompt? )

Thanks! Nancy

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're using .insert() on a list, which requires an index as the first argument. You only gave the content, not the index (well, Python thinks that's the index, but that's a whole different problem). Check help(list.insert) in your shell.