Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Quick help!
#1
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
Reply
#2
"items" in your case is a list, you probably meant self.menu[item], so "item" minus the "s"
Reply
#3
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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A quick question teczone 4 4,684 Sep-06-2018, 03:44 PM
Last Post: snippsat
  quick way to convert in both 2 and 3 Skaperen 10 12,065 Nov-03-2016, 04:43 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.