[英]what should have been an empty string adds a value
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.好吧,我正在做一个免费的代码营项目,但发生了一些很奇怪的事情 function 与 object 实例完美配合,并且由于另一个创建而不是空字符串而失败,该字符串具有我以前使用过的值但我没有'不知道可能是什么原因我离开了整个代码,但你应该注意的是第 25 行,这是导致错误的原因,相关函数被撤回我离开了整个代码,因为通过执行我假设的这么多函数错误可能来自另一个,所以我想确保信息是完整的。
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]))
The problem is in the __str__()
method, not withdraw()
.问题出在
__str__()
方法中,而不是withdraw()
中。
When i
is an empty string, the for char in i:
loop doesn't execute, so it doesn't assign amount_str
.当
i
是空字符串时, for char in i:
不会执行,因此它不会分配amount_str
。 As a result, amount_str
will still contain the value from the previous iteration.因此,
amount_str
仍将包含上一次迭代的值。 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.不需要
amount_str
变量,它与amount
相同,您在每次迭代开始时将其正确初始化为空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.