Basta Fazoolin, Error: 'str' object has no attribute 'start_time'

https://www.codecademy.com/courses/learn-python-3/projects/basta-fazoolin

I have problem with 18 exrecise from Basta Fazoolin project. When I trying to check available menus, I got error:

Traceback (most recent call last):
  File "script.py", line 59, in <module>
    print(flagship_store.available_menus(datetime.time(12,0)))
  File "script.py", line 13, in available_menus
    if time >= menu.start_time and time <= menu.end_time:
AttributeError: 'str' object has no attribute 'start_time'

I can’t understand what’s wrong.
My code:

import datetime
class Franchise:
  def __init__(self, address, menus):
    self.address = address
    self.menus = menus
    
  def __repr__(self):
    return "Addres: {0}".format(self.address)
  
  def available_menus(self, time):
    av_menu = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:
         av_menu.append(menu)
    return av_menu
      
    
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 + " available from " + self.start_time + " to " + self.end_time
  #def __repr__(self):
  #  return "Name: {0}, Time: {1} - {2}".format(self.name,(self.start_time), (self.end_time)) 
  
  def calculate_bill(self, purchase_items):
    total = 0
    for x in purchase_items:
      total += self.items[x]
    return total
  
flagship_store = Franchise("1232 West End Road", ["brunch","early_bird","dinner","kids"])    
new_installment = Franchise("12 East Mulberry Street", ["brunch","early_bird","dinner","kids"])

    
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.time(11,0),datetime.time(16,0))

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.time(15,0),datetime.time(18,0))

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.time(17,0),datetime.time(23,0))

kids = Menu("kids",{
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}, datetime.time(11,0),datetime.time(21,0))

print(brunch.calculate_bill(["pancakes","home fries","coffee"]))
print(early_bird.calculate_bill(['salumeria plate',"mushroom ravioli (vegan)"]))

print(flagship_store.available_menus(datetime.time(12,0)))

Hi @feodanciy

Python is telling you what the error is.

  File "script.py", line 13, in available_menus
    if time >= menu.start_time and time <= menu.end_time:
AttributeError: 'str' object has no attribute 'start_time'

The value of flagship_store.menus is ["brunch","early_bird","dinner","kids"].

What type is each of the objects in that list? They’re strings.

Do you see why you can’t get the value of the attribute start_time from those objects? Strings have no start_time attribute. I suspect you’re supposed to pass in instances of your Menu class.

Hope that helps.

2 Likes

I am having the same(ish) issue, getting the error

Traceback (most recent call last):
  File "script.py", line 49, in <module>
    print(flagship_store.start_time)
AttributeError: 'Franchise' object has no attribute 'start_time'

But I thought I’d passed in the menu objects when creating the franchises. Code is

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 "{} menu is available from {} to {}".format(self.name,self.start_time,self.end_time)
  
  def calculate_bill (self,purchased_items):
    bill = 0
    for item in purchased_items:
      bill += self.items[item]
    return bill
      
    
  
brunch_menu = 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 = 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 = 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 = Menu("Kids",{
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
},11,21)

menus = [brunch_menu,early_bird_menu,dinner_menu,kids_menu]

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 = []
    for item in self.menus:
      if time >= self.start_time and time <= self.end_time:
        available_menus.append(item)
        return available_menus
  
    
flagship_store = Franchise("1232 West End Road", menus)

new_installment = Franchise("12 East Mulberry Street", menus) 

print(flagship_store.start_time)

What am I doing wrong?

Hello @harry.c.mcintyre, and welcome to the Codecademy Forums!

You did do that properly.

This is the line where the problem was reported by the Python interpreter:

print(flagship_store.start_time)

The variable flagship_store refers to an instance of Franchise, which does not have an instance variable or other attribute named start_time, and that is the cause of the problem. You need, instead, to call the available_menus method from the flagship_store variable, passing a specific time as an argument.

Also note that within the methods of the Franchise class, the self parameter refers to whatever is the current instance of Franchise. However, you have this in the available_menus method of that class:

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

Consequently, self.start_time and self.end_time attempt to access instance variables that do not exist for a Franchise.

The loop header is as follows:

    for item in self.menus:

On that basis, item rather than self, represents each Menu in self.menus during the iterations.

There may be at least one additional problem. Is the return statement in that method indented as you wanted it?

Edited on November 20, 2019 to suggest a remedy for the AttributeError prior to discussing other issues.

2 Likes

Thank you, that’s a great explanation and I now have it working!