gpt4 book ai didi

Python 错误 : List Object Not Callable with For Loop

转载 作者:行者123 更新时间:2023-11-28 16:43:31 26 4
gpt4 key购买 nike

好吧,我遇到了这个 TypeError: 'list' object is not callable

它在 (if type=='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()

最佳答案

您的问题是在 for i in range(len(accountlist())): 行中您有 accountlist()accountlist 是一个 list,而 () 意味着您正在尝试像调用函数一样调用它。将行更改为 for i in range(len(accountlist)): 并且您应该已准备就绪。

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

 TypeError: 'list' object is not callable

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

关于Python 错误 : List Object Not Callable with For Loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com