0

My brief requires me to create a method that

accepts an amount and description. If no description is given, it should default to an empty string. The method should append an object to the ledger list in the form of {"amount": amount, "description": description}.

I have therefore created this function:

 def deposit(self,amount,description):
   if description == "":
     newdict = {"amount":amount,"description":""}
     self.ledger.append(newdict)
   else:
     newdict = {"amount":amount,"description":description}
     self.ledger.append(newdict)

However, the test clothing.deposit(25.55) clearly only uses one argument, the amount, and expects me to leave a blank description, but as is it returns the error TypeError: deposit() missing 1 required positional argument: 'description'. But if a description is required, I also have to be ready to use it. How do I setup my method so it can use the extra argument, description, but if it isn't given it can safely ignore it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Fred B
  • 99
  • 6
  • 3
    Can we stop using https://stackoverflow.com/questions/101268/hidden-features-of-python as a reason for closing Python questions? Even if the answer is there it's unreasonable to expect someone to be able to find it there. – thumbtackthief Feb 13 '22 at 21:49

1 Answers1

2

In Python, you can assign default values to the arguments of a method definition. Change your method signature to

def deposit(self, amount, description="")

This should give the expected behavior.

gallen
  • 1,252
  • 1
  • 18
  • 25