Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__init__() takes 1 positional argument but 2 were given

Tags:

python

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)
like image 621
c_l0426 Avatar asked Oct 28 '16 20:10

c_l0426


1 Answers

You probably want to redefine BankAccount as

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

     # ...
like image 52
chepner Avatar answered Sep 19 '22 17:09

chepner