Basta Fazoolin' project, loop stops when first match is found

Howdy all,

Here is the link to the project: https://www.codecademy.com/courses/learn-python-3/projects/basta-fazoolin

In this project I should make a loop so thta all the restaurant menus available at a certain hour would be displayed to the client.

However, when the loop finds the first True in the IF code it stops…

Please help this newby :slight_smile:

class Franchise:

def __init__(self, address, menus, name):
    self.name = name
    self.address = address
    self.menu = menus

def __repr__(self):
    return 'Welcome to {name}, our address is {add}'.format(name=self.name, add=self.address)

def available_menus(self, time):
    i = 0
    while i < len(self.menu):
        if time >= self.menu[i].start_time and time <= self.menu[i].end_time:
            return self.menu[i], i
        i += 1

That’ because the ‘return’ stop the execution of the function. So if you need to return more elements of a list, is better to create a new empty list to contain the elements to return, and then return that list.

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

That’s how I managed this situation

Thank you! Was going crazy about this :slight_smile:

1 Like