我正在尝试将字典附加到列表中。但它向我显示名称错误和回溯错误。我尝试编辑名称,但还是一样

I am trying to append a dictionary to a list. but it showing me Name error and Traceback error. I tried editing the name but still the same

这就是我要实现的。

完成 budget.py 中的类别 class。它应该能够根据不同的预算类别(如食物、衣服和娱乐)实例化对象。创建对象时,它们以类别的名称传递。 class 应该有一个名为 ledger 的实例变量,它是一个列表。 class 还应包含以下方法:

接受金额和描述的存款方式。如果没有给出描述,它应该默认为一个空字符串。该方法应以 {"amount": amount, "description": description} 的形式将对象附加到分类帐列表。

我的密码是

class Category:
    ledger =[]
    
    def __init__(self,category):
        print(category)
       

    def deposit(self,amount,description):
        
        self.amount=amount
        self.description=description
        if not description:
            return ""
        else:
        
            dect={"amount":self.amount,"description":self.description}
            print(dect)
            ledger.append(dect)
            self.ledger=ledger
            print(self.ledger)
food=Category("food")
food.deposit(1000,"initial deposit")
but when I am executing it I am getting error message.

Traceback (most recent call last):
  File "C:/Users/Admin/Desktop/Python/budject_app.py", line 23, in <module>
    food.deposit(1000,"initial deposit")
  File "C:/Users/Admin/Desktop/Python/budject_app.py", line 19, in deposit
    ledger.append(dect)
NameError: name 'ledger' is not defined

I don't see a typo in the name and tried different things but still it is not working. 

您应该在 __init__ 中使用 self.ledger,而不是在外部使用 ledger

代码:

class Category:
    
    def __init__(self,category):
        self.ledger =[]
        print(category)
       
    def deposit(self,amount,description):
        self.amount=amount
        self.description=description
        if not description:
            return ""
        else:
            dect={"amount":self.amount,"description":self.description}
            print(dect)
            self.ledger.append(dect)
            # self.ledger=ledger
            print(self.ledger)

food=Category("food")
food.deposit(1000,"initial deposit")
food.deposit(2000,"second deposit")

结果:

food
{'amount': 1000, 'description': 'initial deposit'}
[{'amount': 1000, 'description': 'initial deposit'}]
{'amount': 2000, 'description': 'second deposit'}
[{'amount': 1000, 'description': 'initial deposit'}, {'amount': 2000, 'description': 'second deposit'}]

顺便说一句,如果您不需要在其他 Category 方法中使用它们,则不需要使用 self.amountself.description

代码:

class Category:
    
    def __init__(self,category):
        self.ledger =[]
        print(category)
       
    def deposit(self,amount,description):
        if not description:
            return ""
        else:
            dect={"amount":amount,"description":description}
            print(dect)
            self.ledger.append(dect)
            print(self.ledger)

food=Category("food")
food.deposit(1000,"initial deposit")
food.deposit(2000,"second deposit")

结果:

food
{'amount': 1000, 'description': 'initial deposit'}
[{'amount': 1000, 'description': 'initial deposit'}]
{'amount': 2000, 'description': 'second deposit'}
[{'amount': 1000, 'description': 'initial deposit'}, {'amount': 2000, 'description': 'second deposit'}]