[Budget App] Attribute error when looping through array of objects

Hello,

I am working on the budget app and am having an issue with accessing self.ledger during the custom print function.

https://replit.com/@Michael_Nicol/Budget-App#budget.py

Line 34 - 51:

  def __str__(self):
    # get the title for the output
    outputString = str(self.title)
    # subtract the title length from 30, and then divide that by two for the edges
    edge = (30-len(str(self.title)))/2
    for i in range(0,int(edge)):
      outputString = "*"+outputString
      outputString = outputString+"*"
    for obj in self.ledger:
      print(obj)
    # grabs obj array from self.ledger
      outputDesc = obj.description if len(obj.description) <= 23 else (obj.description)[:23]
      print("Output desc")
      print(outputDesc)
    #   outputNum = str(obj.amount)
    #   if outputNum[0] >=7:
    #     outputNum = outputNum[:7]
    return ""

This produces a syntax error:

AttributeError: 'dict' object has no attribute 'description'

printing the obj shows the key-value pair with description, but it errors.

I’m not sure but maybe try using bracket notation .

obj['description']

edit

Check this article

seems there is a better way than bracket notation also.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.