Type error: object takes no parameters


#1

When I am trying to print my_account I get this error

my_account = BankAccount(“Matthew”)
Type error: object() takes no parameters

I have no clue of what it either means or how to fix it.
If someone could be kind enough to explain what the problem is I would be very grateful

This is the code that I have problems with



 class BankAccount(object):
  balance = 0
  def __init_(self, name):
    self.name = name
    
  def __repr__(self):
    return "This account belongs to %s. Your balance is $%.2f" % (self.name, self.balance)
  
  def show_balance(self):
    print "Your balance is %.2f$" % self.balance
    
  def deposit(self, amount):
    if self.amount <= 0:
      print "Your deposit amount is too low"
      return
    else:
      print "Depositing %.2f to your account" % amount
      self.balance += amount
      show_balanace()
      
  def withdraw(self, amount):
    if amount > self.balance:
      print "Your withdrawal exceeds your balance"
      return
    else:
      print "You are withdrawing %.2f$" % amount
      self.balance -= amount
      show_balance()
      
my_account = BankAccount("name")
print my_account


Thanks


#2

the class doesn’t recognize the init method because you made a typo in the method name


#3

omg hahahha thx man really appreciate it


#4

because the init method wasn’t recognized, python as very surprised you gave an argument ("Matthew") when you initialized the class. Maybe you now also understand the error message better


#5

yeah because without init() the object parameter wouldnt work?


#6

yes, because the parameters are defined at the init method


#7

Basically the object parameter is defined by init() parameters


#8

yes, that is the magic part of __init__(), its the method that gets magically called when creating an instance of a class


#9

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