【问题标题】:what should have been an empty string adds a value应该是空字符串的内容添加了一个值
【发布时间】:2022-01-25 12:25:50
【问题描述】:

好吧,我正在做一个免费的代码营项目,但发生了一件很奇怪的事情但我不知道我留下整个代码下方的原因可能是什么,但你应该注意的地方是第 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

下面的完整代码

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]))

【问题讨论】:

  • 第 25 行是什么?什么字符串是空的?你遇到了什么错误?
  • clothing.withdraw(25.55) 没有指定原因,因此它应该进入附加空字符串的else: 块。
  • self.ledger.append('') # 第 25 行,是的,发生错误的地方是空的,放一个空格和一个第五个
  • [link](shorturl.at/owQV6) 我用来测试的pythontutor在问题发生的步骤63中

标签: python string object


【解决方案1】:

问题出在__str__() 方法上,而不是withdraw()

i 为空字符串时,for char in i: 循环不会执行,因此它不会分配amount_str。因此,amount_str 仍将包含上一次迭代的值。因此,任何没有原因的分类帐条目都将显示上一次迭代的金额。

不需要amount_str 变量,它与amount 相同,您可以在每次迭代开始时正确地将其初始化为空字符串。

【讨论】:

    猜你喜欢
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多