Why doesn't this code work properly?

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

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

menus = [brunch, early_bird, dinner, kids]

class Franchise:
  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 range(len(menus)):
      if menus[menu].start_time <= time <= menus[menu].end_time:
        available_menus.append(menus[menu].name)
        return available_menus

Thanks for your help! :blush:

The contents of “menus” aren’t declared variables, and if they were meant to be strings, they aren’t enclosed in quotation marks.
Hope this helps!

They are declared, I just didn’t include them in the forum because they are very long.

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

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

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

}, 17, 23)

kids = Menu(“kids”,

{

‘chicken nuggets’: 6.50, ‘fusilli with wild mushrooms’: 12.00, ‘apple juice’: 3.00}, 11, 21)

print(brunch)

print(brunch.calculate_bill([“espresso”, “home fries”, “coffee”]))

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

Sorry for not including them initially :cry:

What’s the problem in the code then? :face_with_monocle:

Thanks in advance! :smiling_face_with_three_hearts:

It seems mostly fine to me …
However, I moved

so that this is after the bunch variable (and the other variables used in there) are declared.

Here’s a test run of that code (using the modifications mentioned above):

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 available from {} to {}".format(self.name, self.start_time, self.end_time) def calculate_bill(self, purchased_items): total = 0 for item in range(len(purchased_items)): total += (self.items).get(purchased_items[item]) return total # menus = [brunch, early_bird, dinner, kids] class Franchise: 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 range(len(menus)): if menus[menu].start_time <= time <= menus[menu].end_time: available_menus.append(menus[menu].name) return available_menus 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 }, 11, 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, # comma not needed }, 15, 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, # comma not needed }, 17, 23) kids = Menu("kids", { 'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00 }, 11, 21) menus = [brunch, early_bird, dinner, kids] print(brunch) print(brunch.calculate_bill(["espresso", "home fries", "coffee"])) print(early_bird.calculate_bill(["salumeria plate", "mushroom ravioli (vegan)"]))

To go along with the idea of using Naval time, on the hour, I added another class,

class Half_time:
  def __init__(self, hour):
    self.hour = hour
  def __repr__(self):
    return f"{self.hour % 12 or 12}:00 {['AM', 'PM'][self.hour >= 12 and self.hour < 24]}"

To go along with this I added a pretty_print() method to the Menu class,

  def pretty_print(self):
    s = ""
    for k, v in self.items.items():
      s += f"{k:<42} {v:6.2f}\n"
    return s

Now we can write a decent representation of a Menu instance…

  def __repr__(self):
    return f"{self.name} menu available {Half_time(self.start_time)} to {Half_time(self.end_time)}\n\n{self.pretty_print()}"
Brunch menu available 11:00 AM to 4:00 PM

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 menu available 3:00 PM to 6:00 PM

salumeria plate                              8.00
salad and breadsticks (serves 2)            14.00
pizza with quattro formaggi                  9.00
duck ragu                                   17.50
mushroom ravioli (vegan)                    13.50
coffee                                       1.50
espresso                                     3.00

Dinner menu available 5:00 PM to 11:00 PM

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

Kids menu available 11:00 AM to 9:00 PM

chicken nuggets                              6.50
fusilli with wild mushrooms                 12.00
apple juice                                  3.00