Bank Account: unexpected output


#1


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

The output from my code is not in the correct order. It should first tell me I have a zero balance, then go on to deposit, then withdraw, then show a final balance of 1000.00. Instead, it looks like this...

You are withdrawing: 1000.00
Your balance is now: 1000.00
Account Holder: Justin has a balance of: $1000.00
Account Holder: Justin has a balance of: $0.00
Your balance is now: 0.00
You are depositing: 2000.00
Your balance is now: 2000.00

I'm also still confused about having balance = 0 outside of the init method. What is the benefit of having it outside of the init block? I thought it's related to scope, but I still have to put 'self.' in front of it throughout the class definition.


class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
    
  def __repr__(self):
    return 'Account Holder: %s has a balance of: $%.2f' \
    % (self.name, self.balance)
  
  def show_balance(self):
    print 'Your balance is now: %.2f' % (self.balance)
    
  def deposit(self, amount):
    if amount <= 0:
      print 'ERROR: Amount deposited must be \
      greater than zero.'
    else:
      print 'You are depositing: %.2f' % (amount)
      self.balance += amount
      self.show_balance()
  
  def withdraw(self, amount):
    if amount > self.balance:
      print 'Insufficient funds.'
    else:
      print 'You are withdrawing: %.2f' % (amount)
      self.balance -= amount
      self.show_balance()

my_account = BankAccount('Justin')
print my_account
my_account.show_balance()
my_account.deposit(2000.00)
my_account.withdraw(1000.00)
print my_account


#2

Seems odd, considering the instructions expect our output in a particular order (that makes sense). I was able to reproduce your output...

python bankaccount.py
Balance is 2000
Withdrawn amount: 1000
Balance is 1000
This account belongs to Roy. Balance is 1000
This account belongs to Roy. Balance is  0
Balance is  0
Deposited amount: 2000

Hopefully someone will weigh in with an explanation.


#3

I have encountered a similar error, with output in an unexpected order.

My code:

class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    return "This is %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 "That is not a valid deposit."
      return
    else:
      print "Depositing %.2f." %(amount)
      self.balance += amount
      self.show_balance()
  def withdraw(self, amount):
    if amount > self.balance:
      print "You cannot withdraw that much."
      return
    else:
      print "Withdrawing %.2f." %(amount)
      self.balance -= amount
      self.show_balance()
      
my_account = BankAccount("Alex")
print my_account
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)
print my_account

This outputs:

This is Alex's account. Balance: 1000.00.
This is Alex's account. Balance: 0.00.
Balance: 0.00.
Depositing 2000.00.
Balance: 2000.00.
Withdrawing 1000.00.
Balance: 1000.00.

Here it seems the final print statement appears first, when I would expect the following instead:

This is Alex's account. Balance: 0.00.
Balance: 0.00.
Depositing 2000.00.
Balance: 2000.00.
Withdrawing 1000.00.
Balance: 1000.00.
This is Alex's account. Balance: 1000.00.

I welcome any insight!


#4

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