Inexplicable error bankaccount project


#1



I get the following error message:

Traceback (most recent call last):
File "bankaccount.py", line 39, in
my_account = BankAccount("Hugo")
TypeError: object() takes no parameters

which refers to the following line of code:
my_account = BankAccount("Hugo")

I can't figure out what I did wrong. Anyone out there being able to support?


class BankAccount(object):
  balance = 0
  def __init___ (self, name):
    self.name = name
    
  def __repr__ (self):
    return "%s's account. Balance: $%.2f" % (self.name, self.balance)

  def show_balance (self):
    print "Balance: $%.2f" % (self.balance)
    
  def deposit (self, amount):
    if amount <= 0:
      print "Please enter a positive amount you want to deposit."
      return
    else:
      print "You deposit $%.2f" (self.amount)
      self.balance += amount
      self.show_balance()
    
  def withdraw (self, amount):
    if amount > self.balance:
      print "You cannot exceed your current balance."
      return
    else:
      print "You are withdrawing $%.2f" (self.amount)
      self.balance -= amount
      self.show_balance()

my_account = BankAccount("Hugo")

print my_account
my_account.show_balance()
my_account.deposit (2000)
my_account.withdraw (1000)
print my_account


#2

Hi, @bleikera ,

When you instantiate an object, as follows ...

my_account = BankAccount("Hugo")

... the Python interpreter will call the __init__ method for that type of that object, if an __init__ exists for it. If the class definition for that type does not explicitly include a definition for an __init__ method, then if that class inherits from a parent class that has an __init__ method, that __init__ method is inherited, and gets called.

Check here whether you have created an __init__ method with a name that begins and ends with exactly two underscores ...

def __init___ (self, name):

If not, then your BankAccount class will not be considered by the Python interpreter as specifying its own __init__ method, and will inherit the __init__ method of object. That method does not include a name parameter, and will crash when you supply an argument within the parentheses.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.