Basta Fazoolin

Hi All,

I am struggling to understand the available_menus method. In the solution, it is explained that the ‘menu’ is an object of the class Menu. How can menu be an object when class Menu starts with capital ‘M’.

and why self.start_time is not working

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

Learn Python 3 | Codecademy

class Franchise:
  def __init__(self, address,menus):
    self.address=address
    self.menus=menus
  
  def __repr__(self,):
    return self.address
  
  def available_menus(self,time):
    av_menus=[]
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:
        av_menus.append(menu)
    return av_menus

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 "{} menu is available from {} to {}".format(self.name,self.start_time,self.end_time)
  
  def calculate_bill(self,purchased_items):
    total=0
    for purchase in purchased_items:
      if purchase in self.items:
        total+= self.items[purchase]
    return total

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
}

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,
}

dinner_items= {
  'crostini with eggplant caponata': 13.00, 'caesar salad': 16.00, 'pizza with quattro formaggi': 11.00, 'duck ragu': 19.50, 'mushroom ravioli (vegan)': 13.50, 'coffee': 2.00, 'espresso': 3.00,
}

kids_items= {
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}

brunch= Menu("brunch",brunch_items,1100,1600)
early_bird= Menu("early_bird",early_bird_items,1500,1800)
dinner= Menu("dinner", dinner_items,1700,2300)
kids= Menu("kids",kids_items,1100,2100)

Menus= [brunch,early_bird,dinner,kids]

print(brunch)

print(brunch.calculate_bill(["pancakes","home fries","coffee"]))

print(early_bird.calculate_bill(["salumeria plate","mushroom ravioli (vegan)"]))

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

print(flagship_store.available_menus(1700))

menu is used for an instance of the Menu class;
so the variable menu is an object that has type Menu

I don’t understand how can we create an instance like this.

if we can shouldn’t it be Menu.start_time rather than menu?
also, we did not create any object

can you please explain in detail a bit?

In this case menu is a local variable, used in the class Franchise, in a function, as some sort of a placeholder to loop over a list (which is eventually defined as: Menus= [brunch,early_bird,dinner,kids] ), containing the objects you initialized from the Menu class like the ones below:

brunch= Menu("brunch",brunch_items,1100,1600)
early_bird= Menu("early_bird",early_bird_items,1500,1800)
dinner= Menu("dinner", dinner_items,1700,2300)
kids= Menu("kids",kids_items,1100,2100)

In a way it would be right to call the variable menu an object, because it’s just you telling python to loop over this list of objects (in this case created from the Menu class).

Classes are a tough subject to get your head around. Good luck and I hope I was able to help you a bit.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.