BankAccount Project getting error


#1

I am getting an error:
Traceback (most recent call last):
line 28, in
print my_account
TypeError: str returned non-string (type NoneType)

I also checked the video it has the same code, however I get an error. Maybe I am missing something.

here is my code:


#2

line 28 triggers which method? __repr__() indeed.

the error tells us this method should return a string, and that currently nothing is returned (None means the absence of a return value)


#3

I found the bug. In line 6, I had print " " instead of return " ".

So it runs now, but there is a different problem :expressionless:
it doesn’t save the balance when i use print my_account. It is always the same statement.
but when I use show_balance () it shows the right balance.
And sometimes it prints the 2 decimals and sometimes not.


#4

this is the output i get:

This account belongs to Mike and has a balance of $ 0.00
Your total balance is $0.00
You are depositing $ 2000.00
Your total balance is $2000.00
You are withdrawing $1000.00
Your total balance is $1000.00
This account belongs to Mike and has a balance of $ 1000.00

with this code:


class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    return "This account belongs to %s and has a balance of $ %.2f" % (self.name, self.balance)
  
  def show_balance(self):
    print "Your total balance is $%.2f" % self.balance
  def deposit(self, amount):
    if amount <= 0 :
      print "Invalid deposit"
      return
    else :
      print "You are depositing $ %.2f" % amount
      self.balance += amount
      self.show_balance()
  def withdraw(self, amount):
    if amount > self.balance :
      print "Error, you can't withdraw more than you have dummy!"
      return
    else :
      print "You are withdrawing $%.2f" % amount
      self.balance -= amount
      self.show_balance()

my_account = BankAccount("Mike")
print my_account
my_account.show_balance()
my_account.deposit(2000)
my_account.withdraw(1000)
print my_account

i only changed print to return, and everything seems to work fine?


#5

That is weird. Probably a bug in the codecademy code this time :grin:
Thanks for the help.