Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - __init__() takes 1 positional argument but 2 were given

I've read through other posts regarding this error and I thought I solved the problem, but I'm still having trouble.

I have included the necessary self argument in the appropriate space, but I am still receiving the error:

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You probably want to redefine BankAccount as

class BankAccount(object):
    def __init__(self, init_bal=0):
        self.balance = init_bal

     # ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...