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

问题描述 投票:-1回答:1

我正在完成Codecademy的课程,我坚持一个专注于OOP的项目。我需要将Menu init中的items参数传递给Franchise类。我正在尝试让available_menu()接受时间参数并返回当时可用的菜单。因此,如果我放入中午12点,它将返回早午餐和孩子们。现在,如果我尝试通过Franchise类访问任何菜单中的信息,我将从菜单获取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])
python python-3.x
1个回答
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])
© www.soinside.com 2019 - 2024. All rights reserved.