Python错误:使用For循环无法调用列表对象

时间:2013-05-05 21:40:58

标签: python list class loops typeerror

好吧所以我有这个TypeError:'list'对象不可调用

它位于(如果输入=='D')

之下的for循环中

确切错误如下:

     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

我的代码如下,我已经尝试将每个括号括在括号中,并将列表重命名为不同的内容,总是出现相同的错误。

我在这里做错了什么?

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

1 个答案:

答案 0 :(得分:4)

您的问题是for i in range(len(accountlist())):accountlist()accountlistlist()表示您试图像调用函数一样调用它。将行更改为for i in range(len(accountlist)):,您应该全部设置。

在旁注中,很容易从错误中识别出问题:

 TypeError: 'list' object is not callable

告诉你你需要知道的确切内容:你正试图在第49行“调用”一个列表。学习阅读错误信息是一项重要且有用的技能。