well I'm doing a free code camp project and something quite strange is happening to me a function works perfectly with an object instance and fails with another one creating instead of an empty string a string with a value that I had used before but I don't know what could be the cause I leave below the whole code but where you should pay attention is in line 25 which is the one that causes the error the related functions are withdraw I leave the whole code because by executing so many functions I assumed that the error could come from another so I wanted to make sure that the info is complete.
def withdraw(self, amount, cause=False):
if self.check_funds(amount):
amount = amount * -1
if cause:
self.ledger.append('"amount" : ' str(amount) ' "description" : ' str(cause))
self.value = amount
print(self.ledger)
return True
else:
self.ledger.append('') # line 25
return True
whole code below
class Category:
def __init__(self, categorie):
self.categorie = categorie
self.value = 0
self.ledger = []
print ("el objeto " categorie " ha sido creado")
def deposit(self, amount, cause=False):
if cause:
self.ledger.append('"amount" : ' str(amount) ' "description" : ' str(cause))
self.value = amount
print(self.ledger)
else:
self.ledger.append('')
def withdraw(self, amount, cause=False):
if self.check_funds(amount):
amount = amount * -1
if cause:
self.ledger.append('"amount" : ' str(amount) ' "description" : ' str(cause))
self.value = amount
print(self.ledger)
return True
else:
self.ledger.append('')
return True
else:
return False
def get_balance(self):
return self.value
def check_funds(self, amount):
if amount > self.value:
return False
else:
return True
def transfer(self, amount, category):
if self.check_funds(amount):
category.deposit(amount, "Transfer from " self.categorie)
self.withdraw(amount, "Transfer to " category.categorie)
return True
else:
return False
def __str__(self):
asterisk = 15 - len(self.categorie) / 2
devolver = (("*"*int(asterisk)) self.categorie ("*"*int(asterisk)) "\n")
for i in self.ledger:
description = ""
amount = ""
description = i.find("description")
description = description len("description : ")
description_str = i[description:]
for char in i:
if char in "0123456789.-":
amount = char
amount_str = amount
if len(description_str) < 23:
devolver = description_str
devolver = " "*(30-len(description_str)-len(amount_str))
devolver = amount_str "\n"
else:
devolver = description_str[0:23]
devolver = " "*(30-23-len(amount_str))
devolver = amount_str "\n"
return devolver
def create_spend_chart(categories):
pass
food = Category("Food")
food.deposit(1000, "initial deposit")
food.withdraw(10.15, "groceries")
food.withdraw(15.89, "restaurant and more food for dessert")
print(food.get_balance())
clothing = Category("Clothing")
food.transfer(50, clothing)
clothing.withdraw(25.55)
clothing.withdraw(100)
auto = Category("Auto")
auto.deposit(1000, "initial deposit")
auto.withdraw(15)
print(food)
print(clothing)
print(create_spend_chart([food, clothing, auto]))
CodePudding user response:
The problem is in the __str__()
method, not withdraw()
.
When i
is an empty string, the for char in i:
loop doesn't execute, so it doesn't assign amount_str
. As a result, amount_str
will still contain the value from the previous iteration. So any ledger entry with no cause will show with the amount from the previous iteration.
There's no need for the amount_str
variable, it's the same as amount
, which you correctly initialize to an empty string at the beginning of each iteration.