I'm confused, shouldn't this be the answer?

I’m stuck and I don’t know why this isn’t working.


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Budget App

Link to the challenge:

Notice in the output part where i is printed:

{'amount': 1000, 'description': 'initial deposit'}
{'amount': -10.15, 'description': 'groceries'}
{'amount': -15.89, 'description': 'restaurant and more food for dessert'}
(-50, 'Trasfer to Clothing')

Last entry before error is indeed a tuple, not a dictionary like in other cases. Take a look at places in code where new entries are added to ledger.

1 Like

I suggest you familiarize yourself with the Python traceback, which tells you where and how things have gone wrong. In your case:

Traceback (most recent call last):
  File "main.py", line 19, in <module>
  File "/home/runner/7Qh8qK21lEl/budget.py", line 43, in __str__
    s += i["description"][:23].ljust(23) + str("{:.2f}".format(i["amount"]).rjust(7)) + "\n"
TypeError: tuple indices must be integers or slices, not str

So, print(food) on line 19 of main.py is calling the __str__ method of your Category instance, which is failing on line 43 of budget.py due to a TypeError. Specifically, a tuple is being accessed with a string index, when — as the error message states — tuple indices must be integers or slices, not strings.

1 Like

I read the python traceback, and for some reason, my brain didn’t connect that I can have a dictionary inside a list. So I thought I had to make a tuple with the amount and description and append that to the list, which obviously didn’t work. So when realized that I forgot to remove the parentheses that make it a tuple. I usually check for errors like these using print statements, but I couldn’t really do that since this is a class. That’s also why I forgot that print statement in there while I was debugging!

Anyways thanks for the help!

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