PythonProblem


#1



https://www.codecademy.com/courses/python-ext/projects/bank-account


If I run the program, t although the beginning part of the program works quite well, the last line of the program shows the balance 0 and it is different from the results that we can get from the second last line.


I am getting very confused with why it works like this. Could anyone explain why the program works like this and tell me how to fix the bug please?

Thank you in advance!


"""The program is to virtually create a bankaccount"""
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 "%s's account. Balance: $%.2f" % (self.name, self.balance)
    def deposit(self, amount):
        if amount <= 0:
          return
        else:
          print "Depositing..."
        print float(amount)
        self.balance += amount
        self.show_balance()
    def withdraw(self, amount):
        if int(amount) > int(self.balance):
            print "The amount you can withdraw should be less than or equal to your balance!"
            return
        else:
            print "Withdraw amount: %s" % (amount)
            self.balance -= amount
            self.show_balance()
my_account = BankAccount("Sean")
print my_account
print my_account.show_balance()
a = BankAccount("Sean")
a.deposit(2000)
a.withdraw(500)
print my_account


#2

i am confused about this line:

a = BankAccount("Sean")

why create another class instance? You can simply deposit and withdraw from my_account?


#3

Oh, I didn't notice that :joy:. Never mind. Could you please help solve my problem please?


#4

if you then deposit and withdraw from my_account, it should be good? I didn't see further problems


#5

Sorry but my point is that the outcome of the last step should be 1500. But it turned out 0. I'm confused about this.


#6

because you deposit and withdraw from a class instance (this should be my_account instance), and then you expect change have happened to my_account instance


#7

Oh I finally realize the problem. Thank you!


#8