简体   繁体   English

应该是空字符串的内容添加了一个值

[英]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.

相关问题 计算字符串中的连续字母如果值为 1 那么它应该是空的? - Count the consecutive letter in a string if the value 1 is there then it should be empty? 我可以将列表中的 2 个连续空字符串合并为 1 个空字符串,以便当我们有 4 个空字符串时,它应该合并并生成 2 个空字符串 - Can i merge 2 continuous empty string in a list into 1 empty string, so that when we have 4 empty string then it should merge and make 2 empty string 应该是无限循环 - It should have been an infinite loop 我的嵌套列表总是添加一个空字符串 - My nested list always adds an empty string Python class 将空行添加到最后一个值 - Python class adds empty line to last value Python + cmd行字符串操作我应该做些什么 - Python + cmd line string manipulation what should I have done diferently 使用bs4的Web抓取。 如果字符串没有与之关联的标签,该怎么办 - Web Scraping using bs4. What should I do if a string doesn't have a tag associated with RNG 应该忽略已经给出的数字 [Python] - RNG should ignore numers that have already been given [Python] substring 是初始循环的空字符串,每次循环运行时,新字符串都会添加到 substring 或再次变为空字符串? - the substring is empty string for initial loop , when everytime the loop runs , new string adds to the substring or it becomes empty string again? .write 函数在达到哨兵值后添加一个新行 - the .write function adds a new line after the sentinel value has been reached
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM