Python simulates three-level menu effects,
The examples in this article share with you the code for simulating the three-level menu effect in Python for your reference. The details are as follows:
1. Features
This program simulates multi-level menu operations, enables you to press the corresponding Digital index of the menu item to enter the sub-menu, press B to go back to the top menu, and press q to exit the menu. A three-level menu test is carried out with a simplified specialized subject directory. In fact, this program can be used for any multi-level menu operation.
2. Implementation Method
This program is written in python. to efficiently implement menu operations and reduce the number of loops, the following variables are defined:
Current_menu_dict: A non-lowest-level menu is a nested dictionary, and a list is used as the lowest-level menu to store the current-level and subsequent menus.
Upper_menu_list: a list that stores a list of nested dictionaries at a higher level than the current level. The last element has one more parent menu than current_menu_dict.
Temp_menu: a list that stores only the menus of the current level, that is, the key value of current_menu_dict.
Menu operation process:
(1) press the number key to enter the corresponding sub-menu: The number key is the temp_menu index, and the current menu item is extracted based on the index, that is, a key value of current_menu_dict, current_menu_dict can point to the next menu nested dictionary corresponding to the key value. However, before entering the lower menu, upper_menu_list.append (current_menu_dict) first adds the current menu nested dictionary as the last element of upper_menu_list, prepare for rollback.
(2) press the B key to roll back to the top menu: current_menu_dict = upper_menu_list.pop () to obtain the nested Dictionary of the top menu and implement rollback.
(3) press the q key to exit the menu: Call exit () to exit the program when the q key is pressed.
Example: assume that the level-3 menu dictionary is {level-1 menu: {level-2 menu: Level-3 menu}. If the current menu is level-2
Current_menu_dict = {level 2 menu: Level 3 menu}
Upper_menu_list = [{level 1 menu: {level 2 menu: Level 3 menu}]
Temp_menu = [Level 2 menu]
3. Flowchart
4. Code
# Author: Byron Limenu_data = {'literature ': {'Chinese Language and Literature': ['linguistics and Applied Linguistics ', 'Chinese language and text', 'ancient Chinese literature ', 'Contemporary Chinese literature '], 'foreign language literature': ['English language literature ', 'Asian-African language literature', 'Arabic language literature ', 'Foreign Linguistics and Applied Linguistics '], 'journalism': ['journalism ', 'communication'], 'artbe': ['music', 'art ', 'drama and opera learn', 'Film learn', 'dance learn']}, '': {'mat': ['basic mat', 'computing mat ', 'probability theory and mathematical statistics', 'applied mates', 'operational research and control'], 'physicist ': ['theoretical physical', 'particle physical and nuclear physical ', 'atomic and molecular physical ', 'plasma physical', 'Radio physical '], 'chemistry': ['inorganic chemistry ', 'analytical chemistry', 'organic chemistry ', 'Physical chemic', 'polymer chemistry and physical '], 'bi': ['botany', 'rology ', 'physiology ', 'Microbiology', 'genge'], 'geophysical ': ['solid geophysical', 'space physicist ']}, 'engineering': {'mechanical engine ': ['mechanical manufacturing and automation ', 'mechanical and electronic engine', 'mechanical design and theoretic', 'Vehicle engine'], 'Electrical engine': ['power system and automation ', high Voltage and insulation technology, power electronics and power transmission, electrical theory and new technology], computer science and technology: ['computer system structuring ', computer Software and theory, computer application technology, Civil Engineering: [geotechnical engineering, structural engineering, municipal engineering ', 'disaster prevention and mitigation engineering and protection Project', 'bridge and tunnel project'], 'geological resources and geoengineering ': ['Mineral survey and exploration ', 'Earth detection and Information Technology ', 'geoengine'] }}current_menu_dict = menu_data # stores nested dictionaries for menus of the current level and subsequent levels, at the lowest level, it is a list upper_menu_list = [] # which is more advanced than the current level. For the list of all menu dictionaries, the last element is more than current_menu_dict. The parent menu temp_menu = [] # stores a temporary list of the current menu while (True): for I, subject in enumerate (current_menu_dict): # The index and menu item if type (current_menu_dict) of the current menu are displayed cyclically = dict: # if the current menu is not the lowest level menu, the index and menu item print (''. join (['\ t' * len (upper_menu_list),' [', str (I),'] ', subject]) else: # If the current menu is the lowest level menu, only the menu items are displayed, and the index print (''. join (['\ t' * len (upper_menu_list), subject]) temp_menu.append (subject) choice = input (''. Join (['\ t' * len (upper_menu_list),' >>> ']) # input option if choice. isdigit (): # if the input option is a digital index corresponding to the menu item, go to the next menu of this menu item if type (current_menu_dict) = dict: # if the current menu is not the lowest level menu, enter a digital index to enter the next level menu index = int (choice) if index> = 0 and index <len (temp_menu) of the corresponding menu item ): key = temp_menu [index] upper_menu_list.append (current_menu_dict) # If the input number is included in the menu item index, add the current menu as the last element in the high-level menu list, in the current menu, enter the next menu corresponding to the index: current_menu_dict = current_menu_dict [Key] else: print ('enter an incorrect number. Please enter it again! ') Else: print (' is already the lowest level menu, press B to return to the upper level menu, and press q to exit! ') Elif choice =' B ': # if the input option is "B", return to the upper menu if len (upper_menu_list)> 0: # if the current menu is not the top menu, take the last element in the high-level menu list as the current menu dictionary current_menu_dict = upper_menu_list.pop () else: # If the current menu is already the top menu, you cannot roll back print ('is the top menu and cannot roll back, press the number key to select the menu item to enter the sub-menu, press the q key to exit ') elif choice = 'q': # If the input option is "q ", exit menu print ('exit menu! '. Center (50,' * ') exit () else: print ('input error, please enter' Again) temp_menu = []
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.