1

I was following a tutorial on setting up a class for an example of making a bar bill and cant figure out why i got error when adding new item to the bill

'dict' object has no attribute 'append'

The code

class Bar_tab:

    #dictionary
    menu = {
        'wine':5,
        'beer':2,
        'coke':3,
        'chicken':9,
        'dessert':7
    }

    #set up the class
    def __init__(self):
        #set up empty initial total and item list
        #customer will add items and total will add up
        #these variables will exist within the class
        self.total = 0
        self.items = {}

    #function for add items to tab
    def add(self,item):
        self.items.append(item)
        #add the value from menu dictionary for the 'item'
        self.total += self.menu[item]

    def pay_bill (self,tax,service):
        #tax will only exist within this function in the class
        tax=(tax/100) *self.total
        service=(service/100)*self.total
        total=self.total + tax + service

        for items in self.items:
            print(f'{item} ${self.menu[item]}')

        print(f'Total is ${total}')`

Error was on the self.items.append(item) line

Community
  • 1
  • 1
NoodlexPoodle
  • 29
  • 1
  • 1
  • 3
  • 3
    `append()` is normally done to lists, not dictionaries since they are not ordered. You also need a key and a value, you only have `item`. Check your tutorial again! – cdarke Oct 06 '18 at 07:01
  • 2
    `self.items` is a dictionary. Make it a list and it should work (change `self.item = {}` to `self.item = []`). – Cleb Oct 06 '18 at 07:04
  • 2
    That should also probably be `for item in self.items:` – Matthias Fripp Oct 06 '18 at 07:07
  • Does this answer your question? [Python AttributeError: 'dict' object has no attribute 'append'](https://stackoverflow.com/questions/48234473/python-attributeerror-dict-object-has-no-attribute-append) – kaya3 Feb 22 '20 at 13:44

1 Answers1

2

self.item = {} initializes self.items to an empty dictionary. A dictionary does not have an append() method because its primary purpose is to associate keys with values. Looking at the code, the intention is for self.menu to be a dict (mapping menu items to prices) and self.items to be a list (of bill items), and list does have an append method.

To intialize self.items as an empty list instead, modify the assignment to:

self.item = []
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Thank you for the comments and answer! I edited self.items to be a list but i got another error instead AttributeError: 'Bar_tab' object has no attribute 'append'. I only changed that part def __init__(self): self.total = 0 self.items = [] – NoodlexPoodle Oct 06 '18 at 17:28
  • @NoodlexPoodle Please don't take this the wrong way, but you need to be aware that StackOverflow is not a substitute for learning materials - the idea is not that we debug your code for you, but that we help you with concrete problems that could be relevant to others. If you want to, you can ask a separate question, but please first try debugging the issue yourself, or working through a Python tutorial. – user4815162342 Oct 06 '18 at 17:41
  • ok got it. i just got super confused when the exact same code runs on the youtube video but not on my end. will try to figure it out myself then. Appreciate your responses! – NoodlexPoodle Oct 07 '18 at 04:12
  • @NoodlexPoodle No problem, glad to help. If this issue is resolved, please consider [accepting](https://meta.stackexchange.com/a/5235) the answer. – user4815162342 Oct 07 '18 at 06:21
  • somehow it worked after i tried today! thanks. i think could be because when i previously tried, I did not exit and restart terminal after i edited the code so the cache of the code in terminal didnt change. thank you – NoodlexPoodle Oct 08 '18 at 06:01