在python中将参数从一个类带到另一个类

时间:2018-08-29 18:13:45

标签: python python-3.x

我正在完成有关Codecademy的课程,我被困在一个专注于OOP的项目上。我需要将Menu初始化中的items参数传递给Franchise类。我试图让available_menu()接受一个时间参数并返回该时间可用的菜单。因此,如果我在中午12点放假,它将返回早午餐和孩子们。现在,如果我尝试通过Franchise类访问任何菜单中的信息,我将从Menu(而不是参数)获取repr。不知道如何解决这个问题。

谢谢!

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

  def calculate_bill(self, purchased_items):
    self.purchased_items = purchased_items
    bill = 0
    temp_item = []

    for item in purchased_items:
      if item in self.items:
        temp_item.append(self.items[item])
      else:
        print("That's not on the menu")

    for item in temp_item:
      bill += item
    return bill

class Franchise:

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

  def available_menu(self):
    for item in self.items:

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)


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

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

1 个答案:

答案 0 :(得分:0)

您需要访问available_menu中特定菜单的项目,而不仅仅是特许经营类中不存在的self.items。调整后的示例将打印出flag_ship_store中第一个菜单的所有菜单项。

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

  def available_menu(self, menu):
    for item in menu.items:
        print(item)

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)

flag_ship_store = Franchise("1232 West End Road", [brunch, early_bird, dinner, kids])
new_installment = Franchise("12 East Mulberry Street", [brunch, early_bird, dinner, kids])
flag_ship_store.available_menu(flag_ship_store.menus[0])