Dec-17-2021, 07:03 PM
(This post was last modified: Dec-17-2021, 07:03 PM by BashBedlam.)
You're headed for trouble. If you keep going like that you will have to create a different
Please let me know if you need any further help with this.
if/elif
list for every menu. Let's organize a little differently. In this code we have oneoperate_menu
function that accepts a tuple of two lists. One list is the options to display for the menu and the other is a list of the command associated with those options. You can change the command that any menu item calls by changing the command in the command list. I've included adummy_function
as a place holder for the commands that you will be using.Please let me know if you need any further help with this.
products = ['product_id', 'name', 'producer','category','price', 'stock'] clients = ['cnp', 'last_name', 'first_name', 'age'] def operate_menu (menu_tuple) : acceptable_options = {'1': 0, '2': 1, '3': 2, '4': 3, '0': 4} display_list = menu_tuple [0] command_list = menu_tuple [1] while True : print ('\n----------------------') for item in display_list : print (item) option = input ('Enter your option: ') if option in acceptable_options : exec (command_list [acceptable_options [option]]) else : print (f'\n{option} is not an acceptable option.') def exit_the_program () : print("\n\nFinished!") exit () def dummy_function () : print ('\nThis is just a place holder function.\n') products_menu = (( "[1] Add new product", "[2] Product update", "[3] Delete product", '[4] Go back to main page', "[0] Exit the program."), ('dummy_function ()', 'dummy_function ()', 'dummy_function ()', 'operate_menu (main_menu)', 'exit_the_program ()')) clients_menu = (( "[1] Add new client", "[2] Client update", "[3] Delete client", '[4] Go back to main page', "[0] Exit the program."), ('dummy_function ()', 'dummy_function ()', 'dummy_function ()', 'operate_menu (main_menu)', 'exit_the_program ()')) display_menu = (( "[1] Search list of: ", "[2] Search a: ", "[3] No operation.", '[4] Go back to main page', "[0] Exit the program."), ('dummy_function ()', 'dummy_function ()', 'dummy_function ()', 'operate_menu (main_menu)', 'exit_the_program ()')) reports_menu = (( "[1] Average age of clients: ", "[2] The average price of the products ", "[3] No operation.", '[4] Return to the main menu', "[0] Exit the program."), ('dummy_function ()', 'dummy_function ()', 'dummy_function ()', 'operate_menu (main_menu)', 'exit_the_program ()')) main_menu = (( "[1] Products", "[2] Clients", "[3] Display", '[4] Report', "[0] Exit the program."), ('operate_menu (products_menu)', 'operate_menu (clients_menu)', 'operate_menu (display_menu)', 'operate_menu (report_menu)', 'exit_the_program ()')) operate_menu (main_menu)