Reputation: 31
def cart():
while True:
s = input('Add new item to shopping list: ')
if s != '0':
cart.append(s)
else:
print('Items on your shopping list are:')
print(', '.join(cart))
break
cart = []
cart()
This is my code and when I try to run it gives:
Error
Traceback (most recent call last):
File "__tester__.python3", line 17, in <module>
cart()
TypeError: 'list' object is not callable
Upvotes: 1
Views: 91
Reputation: 185
It is a namespace error, because you have named two things 1. you list cart = []
and also 2. your function def cart(): ...
with the same name. To fix it you need to rename either your function or your list. When you call cart()
you're actually calling the list declared on the line directly above not the function at the top of the file.
Upvotes: 0
Reputation: 36691
You have defined cart
as a function, then redifined it as an empty list. Python will only hold one version of cart
in memory at a time. You can give your function a different name and that should fix your problem.
def run_cart():
while True:
s = input('Add new item to shopping list: ')
if s != '0':
cart.append(s)
else:
print('Items on your shopping list are:')
print(', '.join(cart))
break
cart = []
run_cart()
Upvotes: 2
Reputation: 1927
you just declared cart
as a list
, and then you call it.
change your function name from cart
to process_input
or anything equal, and just call process_input()
def process_input():
while True:
s = input('Add new item to shopping list: ')
if s != '0':
cart.append(s)
else:
print('Items on your shopping list are:')
print(', '.join(cart))
break
cart = []
process_input()
Upvotes: 0