0
class Franchise:

  def __init__(self, address, menus):
    self.address = address
    #self stands in for the object getting created
    #when we create an object, we call on init
    #when the init constructor gets called, it returns a "Franchise" object
    #self is the object getting returned from the constructor
    #we assign self."address" of the object from the constructor equal to the address that got passed into the ( )
    self.menus = menus

  def __repr__(self):
    return self.address

  def available_menus(self, time):
    available_menu = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:  <---- QUESTION HERE
        #menu.start_time, menu is defined in Class Menu
        available_menu.append(menu)
    return available_menu

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:
        item = self.items.get(purchased_item)
        #another way is:
        #item = self.items[purchased_item]
        bill += item
    return bill


#Brunch Menu
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']))

#print(brunch_menu)

#Early Bird Menu
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(['salumeria plate', 'mushroom ravioli (vegan)']))

#Dinner Menu
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)

#Kids Menu
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_menu, early_bird_menu, dinner_menu, kids_menu]

flagship_store = Franchise("1232 West End Road", menus)

new_installment = Franchise("12 East Mulberry Street", menus)

I have started to learn about classes and object and am a bit confused with regards to the code above.

This was the prompt: "Let’s tell our customers what they can order! Give Franchise an .available_menus() method that takes in a time parameter and returns a list of the Menu objects that are available at that time."

"if time >= menu.start_time and time <= menu.end_time"

Why are we allowed to use menu.start_time and menu.end_time.

Is it because we defined 'class Menu' ?

I'm a bit confused as to why we are allowed to use menu.start_time and menu.end_time because I didn't define that anywhere.

For example: self.menus was defined under def init(self, address, menus)

But nowhere was menu.start_time or menu.end_time defined.

3
  • They are defined, in Menu: self.start_time = start_time. Commented Mar 26, 2020 at 15:03
  • menu is defined by the loop for menu in self.menus; on each iteration, it's a different element of that list, that is, a different instance of Menu. Commented Mar 26, 2020 at 15:06
  • in python classes are case sensitive, Menu is different from menu. You could have used i.start_time and i.end_time, replacing menu with i. Still your code would work as explained in the answer provided by @chepner. Commented Mar 26, 2020 at 15:26

1 Answer 1

1

When you created each Franchise, you gave it a list of Menu instances:

menus = [brunch_menu, early_bird_menu, dinner_menu, kids_menu]
flagship_store = Franchise("1232 West End Road", menus)
new_installment = Franchise("12 East Mulberry Street", menus)

Each call to Franchise saves a reference to that list of menus:

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

so that when you call the available_menus method, you can iterate over that list.

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

and each of those instances had its start_time and end_time attributes defined when it was created.

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

    ...

early_bird_menu = Menu('Early Bird', early_bird_items, 1500, 1800)
Sign up to request clarification or add additional context in comments.

2 Comments

Can I ask please, when you say "and each of those instances had its start_time and end_time attributes defined when it was created." You mean the start_time and end_time was created during the class Menu? So self.menu was created under class Franchise and start/end_time was created under class Menu and we are able to combine these two during the for loop?
The call to Menu creates (for example), the instance you assign to early_bird_menu. That instance is put in a list, which is passed to Franchise. That's the list you iterate over in available_menus, and so menu is (on one iteration) bound to the same object as early_bird_menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.