Hey folks,
Worked my way up to the last project of the Classes section, OOP is exciting to say the least
However, during the final project called ‘Basta Fazoolin’ I’ve found myself in a situation I can’t debug, although I did try. I’ve also been following my attempts at a solution by watching the video walk through, still, there’s something off.
Here’s the code so far:
class Franchise:
pass
def __init__(self, address, menus):
self.address = address
self.menus = menus
def __repr__(self):
return self.address
def available_menus(self, time):
available_menus = []
for menu in self.menus:
if time >= menu.start_time and time <= menu.end_time:
available_menus.append(menu)
return available_menus
class Menu:
pass
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 f'{self.name} is available from {self.start_time} to {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]
else:
print('At least one item is not available on the menu.')
return None
return bill
# Brunch Menu
brunch_menu = {
'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('Brunch', brunch_menu, 1100, 1600)
# Early Bird Menu
early_bird_menu = {
'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('Early Bird', early_bird_menu, 1500, 1800)
# Dinner Menu
dinner_menu = {
'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('Dinner', dinner_menu, 1700, 2300)
# Kids Menu
kids_menu = {
'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00,
'apple juice': 3.00
}
kids = Menu('Kids', kids_menu, 1100, 2100)
# All Menu OBJECTS
menus = [brunch_menu, early_bird_menu, dinner_menu, kids_menu]
# 12
flagship_store = Franchise('1232 West End Road', menus)
new_installment = Franchise('12 East Mulberry Street', menus)
print(flagship_store.available_menus(1200))
The issue I have is related to .available_menus (3rd method of Franchise class), upon calling: print(flagship_store.available_menus(1200)) (example used in walkthrough video).
I get the error:
AttributeError: 'dict' object has no attribute 'start_time'
As far as I’m concerned, the .available_menus doesn’t inherit the attributes of the Menus class, a sentiment not shared by the video narrator who says that it does (though I’m not sure why or how).
When I call: print(flagship_store.menus) for example, I do indeed get a list of dictionaries, so the error, as far I’m concerned, makes sense.
Instead of looping through some the menu OBJECTS, I would rather first loop through the Menus themselves – but I can’t figure out.
How it works in the walkthrough video however is beyond me.
Would really appreciate any insight on the matter. Apologies if this post is messy, I tried to keep it in order. I’m quite excited about OOP but this is very new to me.
Thanks in advance.