Basta Fazoolin'

Basta Fazoolin

i’m at the step 16 and got stuck on it

Summary
> 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 available at {}:00 to {}:00".format(self.name, self.start_time, self.end_time)
>   
>   def calculate_bill(self, purchased_items):
>     self.purchased_items=purchased_items
>     total_price=0
>     for x in purchased_items:
>       total_price += self.items[x]
>       print(x)
>     return total_price
>   
> class Franchise(Menu):
>   def __init__(self, address, menus):
>     self.address = address
>     self.menus = menus
>   
>   def __repr__(self):
>     return "The restaurant is located in: {}.".format(self.address)
>   
>   def available_menus(self, time):
>     self.time = time
>     return super().__init__(name, items, start_time, end_time)
> 
> print(Franchise.available_menus(12))

Traceback (most recent call last):
** File “script.py”, line 31, in **
** print(Franchise.available_menus(12))**
TypeError: available_menus() missing 1 required positional argument: 'time’

i get this error, got stuck on this for a day any help will be appreciated

Hi @mryasincengiz,

Instruction 14 asks us to create two Franchise instances.

Before calling the .available_menus() method, create at least one instance of Franchise, for example …

flagship_store = Franchise("1232 West End Road", [brunch, early_bird, dinner, kids])

After that, it is best to use this syntax to call the method …

print(flagship_store.available_menus(12))

You could do it this way instead …

print(Franchise.available_menus(flagship_store, 12))

Either way, you need to specify an instance of Franchise.

when i call

print(flagship_store.available_menus(12))

i get None printed

from datetime import datetime, time
current_time=datetime.now()

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 available at {}:00 to {}:00".format(self.name, self.start_time, self.end_time)
  
  def calculate_bill(self, purchased_items):
    self.purchased_items=purchased_items
    total_price=0
    for x in purchased_items:
      total_price += self.items[x]
      print(x)
    return total_price
  
class Franchise(Menu):
  def __init__(self, address, menus):
    self.address = address
    self.menus = menus
  
  def __repr__(self):
    return "The restaurant is located in: {}.".format(self.address)
  
  def available_menus(self,time):
    self.time = time
    for i in self.menus:
      print(i)

flagship_store = Franchise("1232 West End Road",[brunch, early_bird, dinner, kids])
new_installment = Franchise("12 East Mulberry Street", [brunch, early_bird, dinner, kids])

print(flagship_store.available_menus(11))

Some code seems to be missing. For example, you need to create Menu instances before creating Franchise instances. It might be a good idea to check each of the instructions to make sure all necessary features are implemented as code.

An example of creating a Menu instance is …

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

You have Franchise as a subclass of Menu here …

class Franchise(Menu):

Restaurants offer menus, but restaurants are not kinds of menus. This would be better …

class Franchise:

Within your available_menus method, you are not checking the time intervals during which the Menu instances are available. Try something like this …

  def available_menus(self, time):
    amenus = []
    for menu in self.menus:
      if menu.start_time <= time <= menu.end_time:
        amenus.append(menu)
    return amenus

thank you so much for your help finally it works, i think i didn’t understand the concepts of the classes well enough i’m gonna go back and redo the exercises

1 Like

Let us know how it goes as you complete the project.

Let’s keep in mind that the available_menus method is designed to handle Menus in which the duration of availability is contained entirely within a single day. If we were to offer a "Night Owl" selection that started, let’s say, at 10:00 PM and ended at 2 AM the next day, we would need additional work to handle it. With a 24 hour clock, the starting time would be 22 while the ending time would be 2. Handling this is perhaps beyond the scope of this project, but implementing this feature could serve as extra practice for those who have completed it.

1 Like

How did you use menu.start_time?

How is menu an object of Menu?

Both of these are absolutely basic to the notion of classes, and you should understand them before attempting this exercise.

If they are not clear, I think that you should you carefully review each step of the Classes module before proceeding.

While doing so, be sure to return here to the forum with any questions on material you don’t understand. By the time you return to Basta Fazoolin’, you will find it to be much easier!

1 Like

I’m just confused how this instance variable from different class used here?
And we didn’t create any menu object (class instance from Menu) to use it over here?

When @appylpye showed this:

  def available_menus(self, time):
    amenus = []
    for menu in self.menus:
      if menu.start_time <= time <= menu.end_time:
        amenus.append(menu)
    return amenus

… it was as a method of the Franchise class, not the Menu class, and menus is an attribute of the Franchise class (see Steps 13 & 16), consisting of a list of Menu objects.

Each Menu object has a start_time and end_time attribute.

So, in the exercise, we have five menus, each a Menu object having its own items dictionary, start_time and end_time. They are arapas_menu, brunch, early_bird, dinner, and kids.

And, then when you decide to open a franchise, you must select which of those menus to offer. You put those menus into a list, and then initialize your franchise:

my_franchise = Franchise(my_address, my_menu_lst)

The __init__() method of Franchise sets self.address = my_address, and self.menus = my_menu_lst

… and now you are ready to run that available_menus() method! Just call

print(my_franchise.available_menus("8 AM"))

3 Likes

Thanks alot! Helped me now.

1 Like

I have a problem with available_menus.
When I try to do it this way I got TypeError: ‘>=’ not supported between instances of ‘int’ and ‘datetime.time’:

  def available_menus(self, time):
    available_menus = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:
        available_menus.append(time)
    return available_menus

And when I tried this I got :

  def available_menus(self, time):
    available_menus = []
    for menu in self.menus:
      if datetime.time >= menu.start_time and datetime.time <= menu.end_time:
        available_menus.append(time)
    return available_menus

I get this error: TypeError: ‘>=’ not supported between instances of ‘type’ and ‘datetime.time’

I tried casting int(menu.start_time) and end time but it is also not suported. Is there a way to do it with datetime? Any hint?

EDIT:

I tried changing datetime to int like this:

class Franchise:
  def __init__(self, address, menus):
    self.address = address
    self.menus = menus
  def __repr__(self):
        return "The address of this installment is " + str(self.address)
  def available_menus(self, time):
    available_menus = []
    for menu in self.menus:
      if time >= menu.start_time and time <= menu.end_time:
        available_menus.append(time)
    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 str(self.name) + " is being served from " + str(self.start_time) + " to " + str(self.end_time)
    
  def calculate_bill(self, purchased_items):
    price = 0
    for key, value in self.items.items():
      if key in purchased_items:
        price += value
    print(price)    
      
    

    
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 menu", {
  'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00
}, 11, 21)

menus = [brunch, early_bird, dinner, kids]
flagship_store  = Franchise("1232 West End Road", [brunch, early_bird, dinner, kids])
new_installment = Franchise("12 East Mulberry", [brunch, early_bird, dinner, kids])


print(flagship_store.available_menus(0))
print(flagship_store.available_menus(16))

My output looks like this:

[]
[16, 16, 16]

What the heck?!

Hello, @system2494758475.
The lists being printed come from where? Go back to there, and see how the list is put together. What are the values being appended to the list?
Hint:

I can’t figure out why I am getting this error or resolve it.

# Create a Menu class
# Give Menu a constructor with the five parameters self, name, items, start_time, and 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

    # Give our Menu class a string representation method that will tell you the name of the menu.
    # Also, indicate in this representation when the menu is available.
    def __repr__(self):
        return "{} menu available from {} to {}".format(self.name, self.start_time, self.end_time)

    # Give Menu a method .calculate_bill() that has two parameters: self, and purchased_items, a list of the names of purchased items.
    # Have calculate_bill return the total price of a purchase consisiting of all the items in purchased_items.
    def calculate_bill(self, purchased_items):
        total_price = 0

        for item in purchased_items:
            if item in self.items:
                total_price += self.items[item]

        return total_price


# Let’s create our first menu: brunch.
# Brunch is served from 11am to 4pm.
# The following items are sold during brunch:
items_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}

brunch = Menu("Brunch", items_brunch, 1100, 1600)

# Let’s create our second menu item early_bird.
# Early-bird Dinners are served from 3pm to 6pm.
# The following items are available during the early-bird menu:
items_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, }

early_bird = Menu("Early Bird", items_early_bird, 1500, 1800)

# Let’s create our third menu, dinner.
# Dinner is served from 5pm to 11pm.
# The following items are available for dinner:
items_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, }

dinner = Menu("Dinner", items_dinner, 1700, 1100)

# And let’s create our last menu, kids.
# The kids menu is available from 11am until 9pm.
# The following items are available on the kids menu.
items_kids = {'chicken nuggets': 6.50,
              'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00}

kids = Menu("Kids", items_kids, 1100, 1900)

# Test out Menu.calculate_bill().
# We have a breakfast order for one order of pancakes, one order of home fries, and one coffee.
# Pass that into brunch.calculate_bill() and print out the price.
print(brunch.calculate_bill(["pancakes", "home fries", "coffee"]))

# What about an early-bird purchase?
# Our last guests ordered the salumeria plate and the vegan mushroom ravioli.
# Calculate the bill with .caluclate_bill().
print(early_bird.calculate_bill(["salumeria plate", "vegan mushroom ravioli"]))

# Let’s create a Franchise class.
# Give the Franchise class a constructor.
# Take in an address, and assign it to self.address.
# Also take in a list of menus and assign it to self.menus.


class Franchise(Menu):
    def __init__(self, address, menus):
        self.address = address
        self.menus = menus

    # Give our Franchises a string represenation so that we’ll be able to tell them apart.
    # If we print out a Franchise it should tell us the address of the restaurant.
    def __repr__(self):
        return "The address is {}".format(self.address)

    # Give Franchise an .available_menus() method
    # that takes in a time parameter and returns a list of the Menu objects
    # that are available at that time.
    def available_menus(self, time):
        available_menu = []

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

        return (available_menu)


# Let’s create our first two franchises!
# Our flagship store is located at "1232 West End Road" and our new installment is located at "12 East Mulberry Street".
# Pass in all four menus along with these addresses to define flagship_store and new_installment.
store_flagship = Franchise("1232 West End Road", [items_brunch,
                                                  items_dinner, items_early_bird, items_kids])
store_new_installment = Franchise("12 East Mulberry Street",
                                  [items_brunch, items_dinner, items_early_bird, items_kids])

# Let’s test out our .available_menus() method!
# Call it with 12 noon as an argument and print out the results.
print(store_flagship.available_menus(1200))

line 94, in available_menus
if menu.start_time <= time and menu.end_time >= time:
AttribteError: 'dict' object has no attribute 'start_time'

Couple of things. Is Franchise a sub-class of Menu?

Try printing menu immediately before the if statement. What does it look like? Does it have the requested start_time attribute?

When I do this

    def available_menus(self, time):
        available_menu = []

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

        #return available_menu

It prints items_brunch, items_early_bird, an items_dinner.
(I’m still thinking this out).

Clarify, it prints out the contents of those list.

Right. So, that’s not what you wanted. Where did those values come from?

Well I assumed originally it would originate from

store_flagship = Franchise("1232 West End Road", [items_brunch,
                                                  items_dinner, items_early_bird, items_kids])
store_new_installment = Franchise("12 East Mulberry Street",
                                  [items_brunch, items_dinner, items_early_bird, items_kids])

Which now I see doesn’t have the time in that that class on that object?