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 (2016, retired 2019) Lists Shopping List Take Three

my code keeps giving me "TypeError: 'list' object is not callable" and i dont know why

import os

shopping_list = []

def show_list(): clear_screen()

print ("Here is your shopping list:")

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

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

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 show items in 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 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)
else:
    shopping_list.append(new_item)


show_list()          

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

show_list()

Steven Parker
Steven Parker
231,269 Points

Try formatting your code so the spacing shows up (in Python, indentation is critical).
Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:
Or watch this video on code formatting.

3 Answers

Steven Parker
Steven Parker
231,269 Points

Hey, I think I might have spotted it even while it's unformatted! Is see this line:

    for item in shopping_list():

The parentheses after the name mean "call this function", but "shopping_list" is a list object (and therefore not callable).

Those parentheses should just be removed.

I got this error:

Here's your list:                                                                       
1. apples                                                                               
----------                                                                              
Where should I add grapes?                                                              
Press ENTER to add to the end on the list                                               
>                                                                                       
Traceback (most recent call last):                                                      
  File "shopping_list.py", line 68, in <module>                                         
    add_to_list(new_item)                                                               
  File "shopping_list.py", line 36, in add_to_list                                      
    shopping_list.insert(position-1, item)                                              
TypeError: unsupported operand type(s) for -: 'str' and 'int'        
Steven Parker
Steven Parker
231,269 Points

Please start a fresh question, and be sure to include your code along with the error output.

never mind I fixed it.

Steven Parker
Steven Parker
231,269 Points

Congratulations, and happy coding! :wink: