TypeError: __str__ returned non-string (type NoneType )


So my code when called gives me this error: "Hello Blake. Your account balance is $0.00." TypeError: str returned non-string (type NoneType) on line 28
I don't know what is wrong with it. My code looks like the examples in the project, but it just isn't working. Any thoughts?

"""This program will reflect a personal bank account. It will be able to accept deposits, allow withdrawals, show the balance, and show account details."""

class BankAccount(object):
  balance = 0
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    print "Hello %s. Your account balance is $%.2f" % (self.name, self.balance)
  def show_balance(self):
    print "Balance: $%.2f" % (self.balance)
  def deposit(self, amount):
    if amount <= 0:
    	print "ERROR: Cannot deposit less than 0 or 0"
      print "Amount deposited: $%s.2f" % (amount)
      balance += amount
  def withdraw(self, amount):
    if amount > balance:
      print "ERROR: Cannot withdraw more than your account balance"
      print "Amount withdrawn: $%.2f" % (amount)
      balance -= amount
my_account = BankAccount("Blake")
print my_account
print my_account

EDIT: so I figured out what was wrong and it was a few things actually.
First: in repr I used print when that function needs to return some sort of value, so I switched print to return and that problem went away.
Second: as mtf pointed out I was missing a set of parenthesis, but I looked at my code and I was missing a few so I fixed those and now it works (basically any time I said self.show_balance)
third: I kept using the variable balance when I should have been referencing self so I changed those to self.balance and now my code works.

Since I'm pointing out my mistakes I also had a formatting error at "Amount deposited: $%s.2f" it should be $%.2f


This should be a method call, invoked with ().


Thanks for that. This made me go back and recheck my code for small things like this and now it works.


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