0
 class Account:
 def __init__(self, initial):
 self.balance = initial
 def deposit(self, amt):
 self.balance = self.balance + amt
 def withdraw(self,amt):
 self.balance = self.balance - amt
 def getbalance(self):
 return self.balance
a = Account(1000.00)
a.deposit(550.23)
a.deposit(100)
a.withdraw(50)
print a.getbalance()

I get this error when I run this code.. AttributeError: Account instance has no attribute 'deposit'

asked Jun 12, 2010 at 0:47
0

4 Answers 4

5
class Account:
 def __init__(self, initial):
 self.balance = initial
 def deposit(self, amt):
 self.balance = self.balance + amt
 def withdraw(self,amt):
 self.balance = self.balance - amt
 def getbalance(self):
 return self.balance

The way you defined them, they were local to the __init__ method, and thus useless.

answered Jun 12, 2010 at 0:48
Sign up to request clarification or add additional context in comments.

Comments

2

So what the above answers mean is that instead your code should be like this - remember unlike other languages, indentation is serious business in Python:

class Account(object):
 def __init__(self, initial):
 self.balance = initial
 def deposit(self, amt):
 self.balance += amt
 def withdraw(self, amt):
 self.balance -= amt
 def getbalance(self):
 return self.balance
a = Account(1000.00)
a.deposit(550.23)
a.deposit(100)
a.withdraw(50)
print a.getbalance()

and now you'll get 1600.23 instead of an error.

answered Jun 13, 2010 at 2:04

Comments

2

You've indented them too deep. They're inner functions of the __init__() method.

answered Jun 12, 2010 at 0:48

Comments

0

In addition to what others have remarked:

You have not correctly shown the code that you actually ran. What appears here has the def __init__ ... at the same level as the class statement; this would have caused a (compile time) SyntaxError, not a (run time) AttributeError.

answered Jun 13, 2010 at 2:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.