对不起,我的英语很差。 我是蟒蛇的新手。 我下面的代码似乎不起作用。我不知道为什么:
import datetime
# class Franchise: def init(self, address, menus): self.address = address self.menus = menus
class Franchise:
def __init__(self, address, menus):
self.address = address
self.menus = menus
def __repr__(self):
return "Our address is {address}".format(address = self.address)
def available_menus(self, time):
list_menus = []
for menu in self.menus:
if menu.start_time <= time <= menu.end_time:
list_menus.append(menu)
return list_menus
# `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`
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} menu available from {start} to {end}".format(name = self.name, start = self.start_time, end = self.end_time)
def calculate_bill(self, purchased_items):
bill = 0
for item in purchased_items:
if item in self.items:
bill += self.items.get(item)
return bill
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
}, datetime.datetime.strptime('11', '%H').strftime('%I %p'), datetime.datetime.strptime('16', '%H').strftime('%I %p'))
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,
}, datetime.datetime.strptime('15', '%H').strftime('%I %p'), datetime.datetime.strptime('18', '%H').strftime('%I %p'))
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,
}, datetime.datetime.strptime('17', '%H').strftime('%I %p'), datetime.datetime.strptime('23', '%H').strftime('%I %p'))
kids = Menu("kids", {
'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}, datetime.datetime.strptime('11', '%H').strftime('%I %p'), datetime.datetime.strptime('21', '%H').strftime('%I %p'))
flagship_store = Franchise("1232 West End Road", ['brunch', 'early_bird', 'dinner', 'dinner', 'kids'])
new_installment = Franchise("12 East Mulberry Street", ['brunch', 'early_bird', 'dinner', 'dinner', 'kids'])
print(new_installment.available_menus(12))
我不断收到以下错误:
属性错误:"str"对象没有属性"start_time">
有人可以在这里帮助我吗? 我在这里错过了什么?
Franchise
构造函数接收Menu
对象列表,而不是字符串。
改变:
flagship_store = Franchise("1232 West End Road", ['brunch', 'early_bird', 'dinner', 'dinner', 'kids'])
new_installment = Franchise("12 East Mulberry Street", ['brunch', 'early_bird', 'dinner', 'dinner', 'kids'])
自:
flagship_store = Franchise("1232 West End Road", [brunch, early_bird, dinner, dinner, kids])
new_installment = Franchise("12 East Mulberry Street", [brunch, early_bird, dinner, dinner, kids])