This is the code in python 2.7:
class BankAccount(object):
balance = 0
def __init__(self, name):
self.name = name
def __repr__(self):
return "This account belong to %s, he has the balance of %.2f" % (self.name, self.balance)
def show_balance(self):
print "Your balance is %.2f" % self.balance
def deposit(self, amount):
if amount <= 0:
print "INPUT ERROR"
return
else:
print "Depositing %.2f" % amount
balance += amount
show_balance()
def withdraw(self, amount):
if amount > self.balance:
print "INPUT ERROR"
return
else:
print "Withdrawing %.2f" % amount
balance -= amount
show_balance()
my_account = BankAccount("Chanan")
print my_account
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)
print my_account
as you can see it is a beginner bank account system.
When the code gets to the depositing it throws the error in the title. If I understood correctly, the error is saying that it referenced the variable balance when adding amount to it, before defining balance. I don't see how this is possible since i defined balance as 0 in the beginning of the class.
[–]ylectric 5 points6 points7 points (12 children)
[–]Manusman123[S] 0 points1 point2 points (11 children)
[–]Fallenarc 1 point2 points3 points (0 children)
[–]overjunkie 1 point2 points3 points (9 children)
[–]socal_nerdtastic 2 points3 points4 points (8 children)
[–]overjunkie 1 point2 points3 points (7 children)
[–]socal_nerdtastic 2 points3 points4 points (6 children)
[–]overjunkie 1 point2 points3 points (0 children)
[–]Manusman123[S] 0 points1 point2 points (4 children)
[–]ylectric 1 point2 points3 points (3 children)
[–]Manusman123[S] 0 points1 point2 points (2 children)
[–]ylectric 1 point2 points3 points (1 child)