Python Forum

Full Version: Quick help!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone,
This is my first time posting here but I am trying to import this class on to the interpreter and it keeps telling me I have a syntax error but I just can't see it. This is from a course on youtube I am following, Thanks to anyone that helps here is the code:

class Tab:

    menu = {
        "wine": 5,
        "beer":3,
        "soft_drink": 2,
        "chicken":10,
        "beef":15,
        "veggie":12,
        "dessert":6
    }

    """docstring for Tab."""
    def __init__(self):
        self.total = 0
        self.items = []

    def add(self, item):
        self.items.append(item)
        self.total += self.menu[item]


    def print_bill(self,tax,service):
        tax = (tax/100) * self.total
        service = (service/100) * self.total
        total = self.total + tax + service
        for item in self.items:
            print(f'{item} £{self.menu[items]}')

        print(f'{"Total"} £{total:.2f}')
The way I am calling this is with: from name_of_file import Tab.
The error is showing up on line 28 just before the last bracket
"items" in your case is a list, you probably meant self.menu[item], so "item" minus the "s"
Also, which python version do you use? This formatting method is supported in Python 3.6+ If you use 3.5 this way will not work and will throw something like this:
Error:
File "<ipython-input-49-81c34fb8cbaf>", line 14 print(f"{item} €{self.menu[item]}") ^ SyntaxError: invalid syntax
And also the point that j.crater mentioned :)