How do I get a list of available menus in this Franchise class from the learn python 3 course?

exercise: https://www.codecademy.com/courses/learn-python-3/projects/basta-fazoolin

my code:

import datetime

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 "The menu name is {name}. It starts at {start} and ends at {end}.".format(name=self.name, start=self.start_time, end=self.end_time)

  def calculate_bill(self, purchased_items):

    self.purchased_items = purchased_items

    empty_list = []

    for food in purchased_items:

      empty_list.append(self.items[food])

    calculate_bill = sum(empty_list)

    return calculate_bill

class Franchise:

  def __init__(self, address, menus):

    self.address = address

    self.menus = menus

  def __repr__(self):

    return "The restaurant's address is {address}".format(address=self.address)

  def available_menus(time):

    if time >=11 and time <=14:

      return brunch.items, kids.items

    elif time >=15 and time <=16:

      return brunch.items, early_bird.items, kids.items

    elif time >=17 and time <=18:

      return early_bird.items, dinner.items, kids.items

    elif time >=19 and time <=21:

      return dinner.items, kids.items

    elif time >=22 and time <=23:

      return dinner.items

     

class Business:

  def __init__(self, name, franchises):

    self.name = name

    self.franchises = franchises

items_for_brunch = {

  '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

}

items_for_early_bird = {

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

}

items_for_dinner = {

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

}

items_for_kids = {

  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00

}

brunch = Menu("Brunch", items_for_brunch, '{:%I:%M%P}'.format(datetime.time(11)), '{:%I:%M%P}'.format(datetime.time(16)))

early_bird = Menu("Early Bird", items_for_early_bird, '{:%I:%M%P}'.format(datetime.time(15)), '{:%I:%M%P}'.format(datetime.time(18)))

dinner = Menu("Dinner", items_for_dinner, '{:%I:%M%P}'.format(datetime.time(17)), '{:%I:%M%P}'.format(datetime.time(23)))

kids = Menu("Kids", items_for_kids, '{:%I:%M%P}'.format(datetime.time(11)), '{:%I:%M%P}'.format(datetime.time(21)))

flagship_store = Franchise("1232 West End Road", [brunch.items, early_bird.items, dinner.items, kids.items])

new_installment = Franchise("12 East Mulberry Street", [brunch.items, early_bird.items, dinner.items, kids.items])

#print(flagship_store)

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

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

print(Franchise.available_menus(17))

firstbusiness = Business("Basta Fazoolin' with my Heart", ["flagship_store", "new_installment"])

arepas_items= {

  'arepa pabellon': 7.00, 'pernil arepa': 8.50, 'guayanes arepa': 8.00, 'jamon arepa': 7.50

}

arepas_menu = Menu("Take a' Arepa", arepas_items, '{:%I:%M%P}'.format(datetime.time(10)), '{:%I:%M%P}'.format(datetime.time(20)))

arepas_place = Franchise("189 Fitzgerald Avenue", [arepas_menu])

newbusiness = Business("Take a' Arepa", [arepas_place])

print(newbusiness.franchises[0].menus[0])

See this part:

  def available_menus(time):

    if time >=11 and time <=14:

      return brunch.items, kids.items

    elif time >=15 and time <=16:

      return brunch.items, early_bird.items, kids.items

    elif time >=17 and time <=18:

      return early_bird.items, dinner.items, kids.items

    elif time >=19 and time <=21:

      return dinner.items, kids.items

    elif time >=22 and time <=23:

      return dinner.items

I ended up doing this as I could not find an alternative. Afterwards I even tried substituting this with code from the video tutorial (which pretty much looked like what I remember typing):

def available_menus(self, time):

    availablemenu = []

    for menu in self.menus:

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

        availablemenu.append(menu)

    return availablemenu

If I then do something like print(flagship_store.available_menus(19)) etc., I get this error:

Traceback (most recent call last):
File “script.py”, line 92, in
print(flagship_store.available_menus(19))
File “script.py”, line 28, in available_menus
if time >= menu.start_time and time <= menu.end_time:
AttributeError: ‘dict’ object has no attribute ‘start_time’

Googling the error is just confusing me. To be honest, I’m already confused after this whole class thing. Could someone explain to me what is going wrong and please show what to change etc. Pretty tired after all this tbh

changed this code too as i vaguely understand what the error meant:

flagship_store = Franchise("1232 West End Road", [brunch, early_bird, dinner, kids])

new_installment = Franchise("12 East Mulberry Street", [brunch, early_bird, dinner, kids])

now i get:

Traceback (most recent call last):
  File "script.py", line 92, in <module>
    print(flagship_store.available_menus(19))
  File "script.py", line 28, in available_menus
    if time >= menuu.start_time and time <= menuu.end_time:
TypeError: '>=' not supported between instances of 'int' and 'str'

changed this too and it now works:

brunch = Menu("Brunch", items_for_brunch, 11, 16)

early_bird = Menu("Early Bird", items_for_early_bird, 15, 18)

dinner = Menu("Dinner", items_for_dinner, 17, 23)

kids = Menu("Kids", items_for_kids, 11, 21)

However I got rid of the fancy way of saying the time :/. Would there be any way to keep this and still get the method to work?

Glad you managed to carry on and get most of it working. I’m not exactly sure what the fancy route for saying the time was but the TypeError pops up if you’ve tried to compare an int and a str object with a comparison operator (it mentions >=). This could be a sorting step which fails when comparing unlike types but in this case it’s probably because one of your attributes is not what you expected it to be. See the line references in the traceback and double check the type of those attributes.

With great stubbornness comes great difficulty. I wanted to use datetime.now() and store datetime objects and the datetime documentation seemed very complicated to me. I had difficulty converting datetime objects into integers for comparison and eventually realized I should be using time objects which can be compared. At least I think that’s what the main problem went, seemed like every solution led to another problem on this one. Take a look at this if you’d like to see my approach:

from datetime import time, datetime
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 f"{self.name} menu is served from {self.start_time.strftime('%I:%M %p')} to {self.end_time.strftime('%I:%M %p')}"
  
  def calculate_bill(self, purchased_items):
    total = 0
    for item in purchased_items:
      try:
        total += self.items[item.lower()]
      except:
        return f"{item.title()} is not on the {self.name} menu."
    return total

class Franchise:
  def __init__(self, address, menus):
    self.address = address
    self.menus = menus
  
  def __repr__(self):
    return self.address
  
  def available_menus(self, mtime = datetime.now().time()):
    #currently_serving = "We are currently serving:\n"
    currently_serving = []
    for menu in self.menus:
      if mtime >= menu.start_time and mtime < menu.end_time:
        #currently_serving += menu.name + " Menu\n"
        currently_serving.append(menu)
    if len(currently_serving) == 0:
      earliest_menu = time.max
      name_menu = ""
      for opening in self.menus:
        if opening.start_time < earliest_menu:
          earliest_menu = opening.start_time
          name_menu = opening.name
      return f"We are currently closed, please return at {earliest_menu.strftime('%I:%M %p')} for our {name_menu} Menu."
    else:
      serving = "We are currently serving: \n"
      for i in range(len(currently_serving)):
        serving += currently_serving[i].name + " Menu \n"
      return serving
  
class Business:
  def __init__(self, name, franchises):
    self.name = name
    self.franchises = franchises


#print(brunch.calculate_bill('coffee'))

brunch = Menu("Brunch", {'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}, time(11), time(16))

early_bird = Menu("Early Bird", {'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}, time(15), time(18))

dinner = Menu("Dinner", {'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}, time(17), time(23))

kids = Menu("Kids", {'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00}, time(11), time(21))

arepas_menu = Menu("Take a’ Arepa", {'arepa pabellon': 7.00, 'pernil arepa': 8.50, 'guayanes arepa': 8.00, 'jamon arepa': 7.50}, time(10), time(20))

flagship_store = Franchise("1232 West End Road", [brunch, early_bird, dinner, kids])

new_installment = Franchise("12 East Mulberry Street", [brunch, early_bird, dinner, kids])

arepas_place = Franchise("189 Fitzgerald Avenue", [arepas_menu])

basta_fazoolin = Business("Basta Fazoolin' with my Heart", [flagship_store, new_installment])

take_a_arepa = Business("Take a' Arepa", [arepas_place])

#christmas_brunch = Menu("Christmas Brunch Menu", )
#print(arepas_menu)
#print(arepas_menu.calculate_bill(['arepa pabellon', 'jamon arepa', 'jamon arepa']))
#print(early_bird.calculate_bill(['salumeria plate', 'mushroom ravioli (vegan)']))
#print(arepas_place)
#print(datetime.now().strftime('%I:%M %p'))
#print(datetime.now().time())

#ughihatethis = time(6)
#print(arepas_place.available_menus(ughihatethis))
#print(new_installment.available_menus(time(15)))

#x1 = time.max.strftime('%I')
#print(x1)
1 Like