我已经阅读了其他关于这个错误的文章,我认为我解决了这个问题,但我仍然有麻烦。
我把必要的“自我”包含在适当的空间里,但我仍然
接收错误:
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
重新定义为
class BankAccount(object):
def __init__(self, init_bal=0):
self.balance = init_bal
# ...
关于python - __init __()接受1个位置参数,但给出了2个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40312491/