【问题标题】: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我正在尝试将字典附加到列表中。但它向我显示名称错误和回溯错误。我尝试编辑名称但仍然相同
【发布时间】:2021-06-23 02:15:24
【问题描述】:

这就是我想要实现的。

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

一种接受金额和描述的存款方式。如果没有给出描述,它应该默认为一个空字符串。该方法应以 {"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. 

【问题讨论】:

  • 尝试用self.ledger.append(dect)引用ledger.append(dect)

标签: python-3.x nameerror


【解决方案1】:

您应该在__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'}]

【讨论】:

    猜你喜欢
    • 2021-08-25
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2020-04-26
    • 2021-10-22
    相关资源
    最近更新 更多