Basta Fazoolin': date comparison issues!

Hiya! I’m on checkpoint 17 on this project and am having trouble comparing the times to see which menu is available. This could definitely be fixed by imputing the times in int() format, but I would really like to know how this works using datetime modules. The error which is thrown for my code says that one of my time points is a string, I’m not sure at what stage my datetime object would have been converted to a string though! Thanks so much for any help :slight_smile:
Marlon

import datetime

class Menu():
  def __init__(self, name, items, start_time, end_time):
    self.name = name
    self.items = items
    self.start_time = start_time.isoformat(timespec='minutes')
    self.end_time = end_time.isoformat(timespec='minutes')
  
def __repr__(self):
    return "This is the {} menu, served from {} 'til {}.".format(self.name, self.start_time, self.end_time)
  def calculate_bill(self, purchased_items):
    total = 0
    for item in purchased_items:
      total += self.items[item]
    return total

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

  def __repr__(self):
    return self.address
  def available_menus(self, time):
    menus = []
    for menu in self.menus:
      if menu.start_time <= time and menu.end_time >= time: #The comparison between two times - why is 1 a string?
        menus.append(menu)
    return menus

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}
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}
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}
kids_items = { 'chicken nuggets': 6.50, 'fusilli with wild mushrooms': 12.00, 'apple juice': 3.00}

brunch = Menu("Brunch", brunch_items, datetime.time(11), datetime.time(16))
early_bird = Menu("Early Bird", early_bird_items, datetime.time(15), datetime.time(18))
dinner = Menu("Dinner", dinner_items, datetime.time(17), datetime.time(23))
kids = Menu("Kids", kids_items, datetime.time(11), datetime.time(21)) # Serving times input with datetime()

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)
print(flagship_store.available_menus(datetime.time(12))) #Query time input with datetime()

Error:

Hard to say exactly what’s happening without seeing your full code, but the error message does say a good bit. Your available_menus method is attempting to compare a datetime.time object with a string and this can’t be done.

I’ve edited it to show the whole code. Yes I realised that one is a string and the other is a datetime object, my question was more why does it become a string when it was written as a datetime object? Is it perhaps to do with the formatting in the menu class? Thanks for your response mate.

Based on the documentation this would be correct. The isoformat() method returns a string version of the date.

1 Like

Ah yes brilliant, thanks!

1 Like