Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
menu while loop
#12
(Dec-17-2021, 07:03 PM)BashBedlam Wrote: You're headed for trouble. If you keep going like that you will have to create a differentif/eliflist for every menu. Let's organize a little differently. In this code we have oneoperate_menufunction 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_functionas a place holder for the commands that you will be using.
Please let me know if you need any further help with this. Smile
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)
Can you explain this line 'acceptable_options = {'1': 0, '2': 1, '3': 2, '4': 3, '0': 4}'?
'1' is position of element in tuple[1]?
Reply


Messages In This Thread
menu while loop - by 3lnyn0 - Dec-17-2021, 03:13 PM
RE: menu while loop - by BashBedlam - Dec-17-2021, 07:03 PM
RE: menu while loop - by 3lnyn0 - Dec-28-2021, 12:58 PM
RE: menu while loop - by Pedroski55 - Dec-18-2021, 03:56 AM
RE: menu while loop - by 3lnyn0 - Dec-21-2021, 07:17 PM
RE: menu while loop - by snippsat - Dec-18-2021, 10:59 AM
RE: menu while loop - by Gribouillis - Dec-19-2021, 10:59 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 03:24 AM
RE: menu while loop - by Gribouillis - Dec-20-2021, 07:09 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 09:32 PM
RE: menu while loop - by bowlofred - Dec-21-2021, 08:06 PM
RE: menu while loop - by Pedroski55 - Dec-22-2021, 04:30 AM
RE: menu while loop - by BashBedlam - Dec-28-2021, 08:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020