我已经阅读了其他帖子关于这个错误的内容,我认为我解决了问题,但是我仍然遇到了麻烦。
我已经在适当的位置包含了必要的self
参数,但我仍然收到错误信息:
Traceback (most recent call last):
File "...", line 30, in <module>
JohnSmith = CheckingAccount(20000)
File "...", line 18, in __init__
BankAccount.__init__(self, initBal)
TypeError: __init__() takes 1 positional argument but 2 were given
class BankAccount (object):
# define class for bank account
def __init__ (self):
# initialize bank account w/ balance of zero
self.balance = 0
def deposit (self, amount):
# deposit the given amount into account
self.balance = self.balance + amount
def withdraw (self, amount):
# withdraw the given amount from account
self.balance = self.balance - amount
def getBalance (self):
# return account balance
return self.balance
class CheckingAccount (BankAccount):
def __init__ (self, initBal):
BankAccount.__init__(self, initBal)
self.checkRecord = {}
def processCheck (self, number, toWho, amount):
self.withdraw(amount)
self.checkRecord[number] = (toWho, amount)
def checkInfo (self, number):
if self.checkRecord.has_key(number):
return self.checkRecord [ number ]
else:
return 'No Such Check'
# create checking account
JohnSmith = CheckingAccount(20000)
JohnSmith.processCheck(19371554951,'US Bank - Mortgage', 1200)
print (JohnSmith.checkInfo(19371554951))
JohnSmith.deposit(1000)
JohnSmith.withdraw(4000)
JohnSmith.withdraw(3500)
BankAccount.__init__
方法只接受self
作为参数,但你同时还传入了initBal
参数。因此,你要么将BankAccount.__init__
改为接受初始余额的参数,要么停止传递初始余额参数。 - Tadhg McDonald-Jensen