Printing "None"


#1

EDIT: I solved it myself.
Problem was print (my_account.show_balance())

Everything works perfectly except, it prints none


This account belongs to User
Balance: 0.00
None
Depositing: 2000.00
Balance: 2000.00
Withdrawing: 1000.00
Balance: 1000.00

Code is below.



class BankAccount(object):
  """Simple Bank Account programm."""
  balance = 0
  
  def __init__(self, name):
    self.name = name
  
  def __repr__(self):
    return "This account belongs to %s" % (self.name)
  
  def show_balance(self):
    print ("Balance: %.2f" % (self.balance))
  
  def deposit(self, amount):
    if amount <= 0:
      print ("Invalid amount, it must be more than 0.")
      return
    else:
      print ("Depositing %.2f" % (amount))
      self.balance += amount
      self.show_balance()
  
  def withdraw(self, amount):
    if amount > self.balance:
      print ("Insufficient fund.")
      return
    else:
      print ("Withdrawing: %.2f" % (amount))
      self.balance -= amount
      self.show_balance()
      
my_account = BankAccount("User")

print (my_account)

print (my_account.show_balance())

my_account.deposit(2000)

my_account.withdraw(1000)




#2

show_balance doesn’t return anything, so you don’t want to use a print statement before the method call. This is what is causing the None


#3

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