Basta Fazoolin's: task 16-17 –AttributeError: 'dict' object has no attribute 'start_time'

Hi, I got stuck at Basta Fazoolin’s: task 16-17.

When I try to call flagship_store.available_menus(1200) I got the Error:
Traceback (most recent call last):
File “script.py”, line 68, in
flagship_store.available_menus(1200)
File “script.py”, line 30, in available_menus
if time >= menu.start_time and time <= menu.end_time:
AttributeError: ‘dict’ object has no attribute ‘start_time’

My code:

class Menu:
  def __init__(self, name, items, start_time, end_time):
    self.name = name
    self.items = items
    self.start_time = start_time
    self.end_time = end_time
  
  def __repr__(self):
    return self.name + ' menu available from ' + str(self.start_time) + ' to ' + str(self.end_time)

  def calculate_bill(self, purchased_items):
    bill = 0
    for purchased_item in purchased_items:
      if purchased_item in self.items:
        bill += self.items[purchased_item]
    return bill

class Franchise:
  def __init__(self, address, menus):
    self.address = address
    self.menus = menus

  def __repr__(self):
    return self.address

  def available_menus(self, time):
    self.time = time
    avaliable_menu = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:
        avaliable_menu.append(menu)
    return avaliable_menu

#BrunchMenu
brunch_items = {
  'pancakes': 7.50, 'waffles': 9.00, 'burger': 11.00, 'home fries': 4.50, 'coffee': 1.50, 'espresso': 3.00, 'tea': 1.00, 'mimosa': 10.50, 'orange juice': 3.50
}
brunch_menu = Menu('Brunch', brunch_items, 1100, 1600)
#print(brunch_menu.calculate_bill(['pancakes', 'home fries', 'coffee']))

#EarlyBirdMenu
early_bird_items = {
  'salumeria plate': 8.00, 'salad and breadsticks (serves 2, no refills)': 14.00, 'pizza with quattro formaggi': 9.00, 'duck ragu': 17.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 1.50, 'espresso': 3.00,
}
early_bird_menu = Menu('Early Bird', early_bird_items, 1500, 1800)
#print(early_bird_menu.calculate_bill(['mushroom ravioli (vegan)', 'salumeria plate']))

#DinnerMenu
dinner_items = {
  'crostini with eggplant caponata': 13.00, 'ceaser salad': 16.00, 'pizza with quattro formaggi': 11.00, 'duck ragu': 19.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00,
}
dinner_menu = Menu('Dinner', dinner_items, 1700, 2300)

#KidsMenu
kids_items = {
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}
kids_menu = Menu('Kids', kids_items, 1100, 2100)

menus = [brunch_items, early_bird_items, dinner_items, kids_items]

flagship_store = Franchise("1232 West End Road", menus)
new_installment = Franchise("12 East Mulberry Street", menus)

#print(brunch_menu)
#print(flagship_store)
#print(new_installment)
flagship_store.available_menus(1200)

What is the reason of Error?

As per the error text there’s a problem on this line: if time >= menu.start_time and time <= menu.end_time: an attempt was made to access the .start_time attribute of a dictionary object which has thrown an error. It appears menu is a dict rather than an instance of the Menu class. Have you perhaps passed a dictionary at some point instead of a Menu instance? Try hunting down an error working backwards from the given line the warning.

2 Likes