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

在python中将参数从一个类带到另一个类,python,python-3.x,Python,Python 3.x,我正在学习一门关于编解码器的课程,我被困在一个专注于OOP的项目上。我需要将Menu init中的items参数传递给特权类。我试图让available_menu()接受一个时间参数,并返回当时可用的菜单。所以如果我放在中午12点,它将返回早午餐和孩子们。现在,如果我试图通过特许类访问任何菜单中的信息,我将从菜单中获取repr,而不是参数。我不知道如何解决这个问题 谢谢 class Menu: def __init__(self, name, items, start_time, end_t

我正在学习一门关于编解码器的课程,我被困在一个专注于OOP的项目上。我需要将Menu init中的items参数传递给特权类。我试图让available_menu()接受一个时间参数,并返回当时可用的菜单。所以如果我放在中午12点,它将返回早午餐和孩子们。现在,如果我试图通过特许类访问任何菜单中的信息,我将从菜单中获取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])

您需要访问
available\u menu
中的特定菜单项,而不仅仅是特许经营类中不存在的
self.items
。此调整后的示例打印出给定给
flag\u ship\u 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])

代码的格式错误(特许类中的for循环)。请把它修好。此外,当您提出此类问题时,您应该指出代码中遇到问题的确切位置以及问题所在。或者,您可以提供一个。现在您在
available\u菜单中有一个缩进错误
示例应该已经完成。现在只有你知道问题是什么。如果有人复制你的代码来运行它,他们将得到许多错误,这些错误可能与你刚才描述的问题无关。我发现@smac89对这个世界来说仍然是新的,所以我仍在学习类似的小东西!感谢你的建议。它的产出是:煎饼、华夫饼、汉堡、家庭薯条、咖啡、浓咖啡、茶、含羞草、橙汁。我需要它在最后输出时间,每个菜单在实际菜单项之后的两个整数。有什么想法吗?是的,您只需将
print(menu.start\u time)
print(menu.end\u time)
添加到
available\u menu
的末尾(不过不要将其嵌套在for循环中)。