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

Justin Richie
PLUS
Justin Richie
Courses Plus Student 7,254 Points

Executing Python code in 3.4.3 shell

From the python 3.4.3 shell, I am trying to use the Shopping Cart example from "Fun with FUNctions" and am having some trouble

shopping_list = []

def show_help(): print("What should we pick up at the store?") print("Enter DONE to stop. Enter SHOW to see current list")

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

def show_list(): print("Here's your list:") for item in shopping_list: print(item)

show_help()

while True: new_item= input ("> ") if new_item == 'DONE':

    break
add_to_list(new_item)
continue

show_list()

My ERROR is below:

================================ RESTART ================================

What should we pick up at the store? Enter DONE to stop. Enter SHOW to see current list Something Traceback (most recent call last): File "C:/Users/Justin/Documents/Python/function.py", line 24, in <module> add_to_list(new_item) File "C:/Users/Justin/Documents/Python/function.py", line 8, in add_to_list shopping_list.append() TypeError: append() takes exactly one argument (0 given)

any thoughts?

4 Answers

Gavin Ralston
Gavin Ralston
28,770 Points

Take a look here at your function:

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

Right now, you have the add_to_list function accepting an item for an argument. You want to add that item to your shopping list, so you'll need to pass it along to the append() function like so:

shopping_list.append(item)

Give that a try and see what happens.

Gavin Ralston
Gavin Ralston
28,770 Points
def add_to_list(item): 
    shopping_list.append()
    # etc, etc

The traceback tells you this:

shopping_list.append() TypeError: append() takes exactly one argument (0 given)

To get this message to go away, you need to provide an argument to append -- probably the item you passed in, like so:

shopping_list.append(item)

Hope that helps!

Jason Aylward
Jason Aylward
2,318 Points

I think you just need to tell shoppinglist what you would like to append:

shopping_list.append(item)

Justin Richie
PLUS
Justin Richie
Courses Plus Student 7,254 Points

Something like this? I am new to python! THanks for the help.

shopping_list = []

def show_help(): print("What should we pick up at the store?") print("Enter DONE to stop. Enter SHOW to see current list")

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

def show_list(): print("Here's your list:") for item in shopping_list: print(item)

show_help()

while True: new_item= input ("> ") if new_item == 'DONE':

    break
add_to_list(new_item)
continue

show_list()

shopping_list.append(item)