Python 3 project , Basta Fazoolin'

can someone tell me why my code showing this error and how to fix it ? i this is happened when i do the task “available menu”.
“print(flagship_store.available_menus(1200))
File “script.py”, line 11, in available_menus
if time>=menu.start_time and time<=menu.end_time:
AttributeError: ‘dict’ object has no attribute ‘start_time’”

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=[]

menus=[brunch_items, early_bird_items, dinner_items, kids_items]

for menu in self.menus:

  if time>=menu.start_time and time<=menu.end_time:

    available_menus.append(menu)

return available_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

def repr(self):

return self.name+ ' from '+ str(self.start_time)+ " to " + str(self.end_time)

def calculate_bill(self,purchased_items):

bill=0

for purchased_item in purchased_items:

  if purchased_item in self.items:

    bill+=self.items[purchased_item]

return bill 

#11:00 - 16:00

brunch_items={‘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}

brunch_menu=Menu(“brunch”, brunch_items, 1100, 1600)

print(brunch_menu)

print(brunch_menu.calculate_bill([‘pancakes’,‘home fries’, ‘coffee’]))

#1500-1800

early_bird_items={‘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, }

early_bird_menu=Menu(“early_bird”, early_bird_items, 1500, 1800)

print(early_bird_menu.calculate_bill([‘salumeria plate’,‘vegan mushroom ravioli’]))

early_bird_menu=Menu(‘early_bird’, early_bird_items, 1500, 1800)

#1700-2300

dinner_items={‘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}

dinner_menu=Menu(‘dinner’, dinner_items, 1700, 2300)

#1100-2100

kids_items={‘chicken nuggets’: 6.50, ‘fusilli with wild mushrooms’: 12.00, ‘apple juice’: 3.00}

kids_menu=Menu(‘dinner’, kids_items, 1100, 2100)

#14

menus=[brunch_items, early_bird_items, dinner_items, kids_items]

flagship_store=Franchise(“1232 West End Road”, menus)

new_installment=Franchise(“12 East Mulberry Street”, menus)

print(flagship_store.available_menus(1200))

print(flagship_store.available_menus(1600))

Please have a look at How do I format code in my posts? which would help keep the indentation and general format of your code.

If you check the error it states their is no start_time attribute for a dict object. The attribute lookup is the dot syntax, obj.start_time. So you can infer that the object on the right of the dot is actually a dictionary type.

You might be able to work back from there to find out why menu is a dict object instead of the type you expected it to be.

1 Like

thanks , i have formatted my quote . please see below. but i didnt generate any dict type. why it says “dict” ?

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=[]
    menus=[brunch_items, early_bird_items, dinner_items, kids_items]
    for menu in self.menus:
      if time>=menu.start_time and time<=menu.end_time:
        available_menus.append(menu)
    return available_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
    
  def __repr__(self):
    return self.name+ ' from '+ str(self.start_time)+ " to " + str(self.end_time)

  def calculate_bill(self,purchased_items):
    bill=0
    for purchased_item in purchased_items:
      if purchased_item in self.items:
        bill+=self.items[purchased_item]
    return bill 

#11:00 - 16:00
brunch_items={'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}


brunch_menu=Menu("brunch", brunch_items, 1100, 1600)
print(brunch_menu)

print(brunch_menu.calculate_bill(['pancakes','home fries', 'coffee']))

#1500-1800    
early_bird_items={'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, }
early_bird_menu=Menu("early_bird", early_bird_items, 1500, 1800)
print(early_bird_menu.calculate_bill(['salumeria plate','vegan mushroom ravioli']))
early_bird_menu=Menu('early_bird', early_bird_items, 1500, 1800)

#1700-2300
dinner_items={'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}
dinner_menu=Menu('dinner', dinner_items, 1700, 2300)

#1100-2100
kids_items={'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00}
kids_menu=Menu('dinner', kids_items, 1100, 2100)
#14
menus=[brunch_items, early_bird_items, dinner_items, kids_items]
flagship_store=Franchise("1232 West End Road", menus)
new_installment=Franchise("12 East Mulberry Street", menus)


print(flagship_store.available_menus(1200))
print(flagship_store.available_menus(1600))

i did the loop and available_menu method exactly same as unstack youtube video showing. but still having some error . but cant find what is it .

In your code what are the differences between names with _items and _menu ?

1 Like

_items is the dictionary of each menu . _menu is each menu as object and calling the Class Menu 4 arguments.

When you intantiate a new Franchise, what should the menus argument be assigned to? Presumably instances of Menu, no?

1 Like

how should i correct this . i do need to call Class Franchise :frowning:

In the Franchise you’ve created you’ve tried to use an attribute .start_time for an object that just doesn’t have that attribute (it’s actually a dictionary). Why would there be a dictionary instead of the type you expected? Have a very close look at where you created this Franchise.

1 Like

my logic is :

for menu in self.menus:
      if time>=menu.start_time and time<=menu.end_time:
        available_menus.append(menu)

in this loop , i use “menu” because it’s in menus:

 "menus=[brunch_menu, early_bird_menu, dinner_menu, kids_menu]"

And for each menu i have called class Menu :

brunch_menu=Menu("brunch", brunch_items, 1100, 1600)

since there is parameters are assigned as start_time(1100) and end_time(1600) . So i assume , it’s good to use menu.start_time in the loop :

if time>=menu.start_time and time<=menu.end_time

That line is nowhere to be found in the code you posted originally. If you’ve changed your code, please post the updated version along with your current error message if there is one.

sorry please see my updated code below.

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=[]
    menus=[brunch_menu, early_bird_menu, dinner_menu, kids_menu]
    for menu in self.menus:
      if time>=menu.start_time and time<=menu.end_time:
        available_menus.append(menu)
    return available_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
    
  def __repr__(self):
    return self.name+ ' from '+ str(self.start_time)+ " to " + str(self.end_time)

  def calculate_bill(self,purchased_items):
    bill=0
    for purchased_item in purchased_items:
      if purchased_item in self.items:
        bill+=self.items[purchased_item]
    return bill 

#11:00 - 16:00
brunch_items={'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, 'start_time':1100, 'end_time':1600}


brunch_menu=Menu("brunch", brunch_items, 1100, 1600)
print(brunch_menu)

print(brunch_menu.calculate_bill(['pancakes','home fries', 'coffee']))

#1500-1800    
early_bird_items={'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,'start_time':1500, 'end_time':1800 }
early_bird_menu=Menu("early_bird", early_bird_items, 1500, 1800)
print(early_bird_menu.calculate_bill(['salumeria plate','vegan mushroom ravioli']))
early_bird_menu=Menu('early_bird', early_bird_items, 1500, 1800)

#1700-2300
dinner_items={'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, 'start_time':1700, 'end_time':2300}
dinner_menu=Menu('dinner', dinner_items, 1700, 2300)

#1100-2100
kids_items={'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00, 'start_time':1100, 'end_time':2100}
kids_menu=Menu('dinner', kids_items, 1100, 2100)
#14
menus=[brunch_items, early_bird_items, dinner_items, kids_items]
flagship_store=Franchise("1232 West End Road", menus)
new_installment=Franchise("12 East Mulberry Street", menus)


print(flagship_store.available_menus(1200))
print(flagship_store.available_menus(1600))

Why is this line from your available_menus method necessary? You already have a menus property (self.menus), and you aren’t using the variable menus assigned here anywhere in your code. Your for loop is accessing self.menus as one would expect. That line does, however, provide a clue as to where your problem lies. Consider as I and @tgrtim have mentioned previously what you are providing as an argument for your menus property when you instantiate your Franchise instances:

Hint

Compare the two lines:

menus=[brunch_menu, early_bird_menu, dinner_menu, kids_menu] #Menu objects 
menus=[brunch_items, early_bird_items, dinner_items, kids_items] #dictionaries used to create Menu objects
1 Like

i need help with

def calculate_bill(self,purchased_items):

    
    return bill

 #??????