Dec-20-2021, 09:32 PM
(This post was last modified: Dec-20-2021, 09:32 PM by BashBedlam.)
(Dec-20-2021, 07:09 AM)Gribouillis Wrote: @BashBedlam With very little work, you could makeThis would be my best shot at thatMenu_Tree.run_menus()
non recursive. Also add the possibility that aftercommand()
, the menu to run is not themain_menu
.
def dummy_function () : print ('\n The dummy function has finished running.') def say_goodbye () : print ('\nBye for now.') exit () class Menu_Tree : def __init__ (self) : self.all_menus = { 'main_menu' : { "[1] Products": 'products_menu', "[2] Clients": 'clients_menu', "[3] Display": 'display_menu', '[4] Report': 'report_menu', "[0] Exit the program.": say_goodbye}, 'products_menu' : { "[1] Add new product": dummy_function, "[2] Product update": dummy_function, "[3] Delete product": dummy_function, '[4] Go back to main page': 'main_menu', "[0] Exit the program.": say_goodbye}, 'clients_menu' : { "[1] Add new client": dummy_function, "[2] Client update": dummy_function, "[3] Delete client": dummy_function, '[4] Go back to main page': 'main_menu', "[0] Exit the program.": say_goodbye}, 'display_menu' : { "[1] Search list of: ": dummy_function, "[2] Search a: ": dummy_function, '[4] Go back to main page': 'main_menu', "[0] Exit the program.": say_goodbye}, 'report_menu' : { "[1] Average age of clients: ": dummy_function, "[2] The average price of the products ": dummy_function, '[4] Return to the main menu': 'main_menu', "[0] Exit the program.": say_goodbye}} self.run_the_current_command ('main_menu') def run_the_current_command (self, command) : if type (command) == str : self.current_menu = command command = self.run_the_menus (self.all_menus [command]) else : command () self.run_the_menus (self.all_menus [self.current_menu]) def run_the_menus (self, menu) : look_up_table = {label [1: 2]: label for label in menu} print ('\n ==============================') for label in menu : print (' ', label) still_selecting = True while still_selecting : menu_choice = input (' Enter your selection: ') if menu_choice != '' and menu_choice in look_up_table : still_selecting = False command = menu [look_up_table [menu_choice]] else : print (f' "{menu_choice}" is not a valid entry.') self.run_the_current_command (command) if __name__ == '__main__' : Menu_Tree ()