Python Error: List Object Not Callable with For Loop
Your problem is that in the line for i in range(len(accountlist())):
you have accountlist()
. accountlist
is a list
, and the ()
means you're trying to call it like you would a function. Change the line to for i in range(len(accountlist)):
and you should be all set.
On a sidenote, it's easy to recognize your problem from your error:
TypeError: 'list' object is not callable
is telling you exactly what you need to know: that you're trying to "call" a list on line 49. Learning to read error messages is an important and useful skill.
user2305960
Updated on June 05, 2022Comments
-
user2305960 almost 2 years
Alright so i'm having this TypeError: 'list' object is not callable
It's on the for loop below the (if type=='D')
Exact error is as follows:
Traceback(most recent call last): file"test.py", line 55 in <module> main() File "test.py", line 49, in main for i in range(len(accountlist())): TypeError: 'list' object is not callable
My code is below, i've tried putting each parenthesis in brackets and renaming the list to something different, always getting around the same error.
What am i doing wrong here?
class BankAccount: def __init__(self, getbankaccount, inputAmount=0): self.__balance = inputAmount self.__numDeposits = 0 self.__numWithdrawals = 0 self.__totalDeposits = 0 self.__totalWithdrawals = 0 self.__getbankaccount=getbankaccount def getBalance(self): return self.__balance def getNumDeposits(self): return self.__numDeposits def getNumWithdrawals(self): return self.__numWithdrawals def getTotalDeposits(self): return self.__totalDeposits def getTotalWithdrawals(self): return self.__totalWithdrawals def getbankaccount(self): return self.__getbankaccount def Deposit(self,amount): self.__balance = self.__balance + amount self.__numDeposits = self.__numDeposits + 1 self.__totalDeposits = self.__totalDeposits + amount return self.__balance def Withdrawal(self,amount): if (self.__balance >= amount): self.__balance = self.__balance - amount self.__numWithdrawals = self.__numWithdrawals + 1 self.__totalWithdrawals = self.__totalWithdrawals + amount return True else: return False def main(): accountlist=[] numbers=eval(input()) for i in range(numbers): account=input() amount=eval(input()) initial=BankAccount(account, amount) accountlist.append(initial) type=input() while type!='#': if type=='D': account=input() amount=eval(input()) for i in range(len(accountlist())): if(account==accountlist[i].getbankaccount()): index=i accountlist[index].Deposit(amount) Print(amount, type, account) type=input() main()
-
user2305960 almost 11 yearsThat fixed the TypeError, thanks a lot, but it's giving me a SyntaxError: Unexpected EOF while parsing on line 41. Which would be the first amount=eval(input()) below the first for loop, any idea what could of caused this?
-
Nolen Royalty almost 11 years@user2305960 I'm not sure why you'd need
eval
there.eval
is used to evaluate python code that is contained in a string. If you're just looking for an amount from the user, I'd change that toamount=input()
. -
user2305960 almost 11 yearsIsn't eval(input()) used to convert the input into an int?
-
Nolen Royalty almost 11 years@user2305960 I suppose you could do that, it'd be much safer to just do
int(input())
-
user2305960 almost 11 yearsWhy would it be safer than eval? My professor always used eval for int inputs.
-
Nolen Royalty almost 11 years@user2305960 because there's no reason to use
eval
when you can just useint
. Usingeval
would mean that if you typed in something like"__import__('os').system('rm -rf /')"
you'd tell python to execute code to delete all the files on your computer. If you're expecting an int as input, useint()
. This also ensures that your code immediately fails if you pass something other than an int as input, instead of failing when you use that value later. -
Schütze over 4 yearsThis kind of errors are not really well informing to be honest. Like, to derive the meaning from the error is like decrypting enigma at times.