Python program Exercise 2-simulate a three-level menu,

Source: Internet
Author: User

Python program Exercise 2-simulate a three-level menu,
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 menus at the current level and subsequent levels.
  • upper_menu_list: A list that stores a list Of all menu 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 iscurrent_menu_dict.

Menu operation process:
(1) press the number key to enter the corresponding sub-menu: The number key istemp_menuAccording to the index to extract the current menu item, that iscurrent_menu_dictAccordingcurrent_menu_dictYou can re-point to the next menu nested dictionary corresponding to this key value, but before entering the lower menu,upper_menu_list.append(current_menu_dict)Add the nested Dictionary of the current menuupper_menu_listTo prepare for rollback.
(2) press the B key to roll back to the top menu: when you roll backcurrent_menu_dict=upper_menu_list.pop()Obtain the nested Dictionary of the previous menu to implement rollback.
(3) Press q to exit the menu: called when q is pressedexit()Exit the program.

Example: assume that the three-level menu dictionary is{Level 1 menu: {level 2 menu: Level 3 menu }}If the current menu is in 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
1 # Author: Byron Li 2 3 menu_data = {4 'literature ': 5 {6 'Chinese Language & Literature': ['linguistics and Applied Linguistics ', 'Chinese Language & Analytics ', 'ancient Chinese literature ', 'modern and contemporary Chinese literature'], 7' foreign language literature ': ['English language literature', 'Asian-African language literature ', 'Arabic language literature ', 'Foreign Linguistics and Applied Linguistics '], 8 'news communication': ['journalism', 'communication'], 9 'artbe': ['music', 'art ', 'Study of drama and opera ', 'Study of film', 'Study of dance '] 10}, 11 12' ': 13 {14' math ': ['basic mat ', 'computational mat', 'probability theory and mathematical statistics', 'applied mat', 'operational research and control'], 15 'physicist ': ['theoretical physics ', 'particle physics and atomic physics ', 'atomic and molecular physics', 'plasma physical ', 'Radio physical'], 16' chemistry ': ['inorganic chemistry ', 'analytical chemistry ', 'organic chemistry', 'Physical chemistry ', 'polymers Chemistry and physics '], 17 'bi': ['botany', 'rology ', 'physiology', 'microbiology ', 'genge'], 18 'geophysical ': ['solid geophysical ', 'space physicist'] 19}, 20 'engineering ': 21 {22 'mechanical engine': ['mechanical manufacturing and automation ', "Mechanical and Electronic Engineering", "mechanical design and theory", "vehicle engineering"], 23 "Electrical Engineering": ["Power System and Automation", "high voltage and Insulation Technology ', 'electric power electronics and electric drive ', 'electric power theory and new technology'], 24 'computer Science and Technology ': ['computer system structuring', 'computer software and theoretic ', 'computer application technical'], 25' civil engineering ': ['geotechnical Project', 'structural Project', 'municipal Project', 'disaster prevention and mitigation Project ', 'bridge and tunnel project'], 26' geological resources and geoengineering ': ['Survey and exploration of minerals', 'Survey of the earth and Information Technology ', 'geological project'] 27} 28} 29 30 current_menu_dict = menu _ Data # stores the nested Dictionary of menus at the current level and subsequent levels. At the lowest level, it is a list 31 upper_menu_list = [] # stores a list Of all menu dictionaries at a higher level than the current level, the last element has more than current_menu_dict parent menu 32 temp_menu = [] # A temporary list for storing the current menu 33 34 while (True): 35 for I, subject in enumerate (current_menu_dict): # The index and menu item 36 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 37 print (''. join (['\ t' * len (upper_menu_list),' [', str (I),'] ', subject]) 38 39 else: # If the current menu is the lowest level menu, only the menu item is displayed, and the index is not 40 p Rint (''. join (['\ t' * len (upper_menu_list), subject]) 41 temp_menu.append (subject) 42 43 choice = input (''. join (['\ t' * len (upper_menu_list),' >>> ']) # input option 44 if choice. isdigit (): # if the input option is a digital index corresponding to the menu item, enter the menu item level 45 if type (current_menu_dict) = dict: # If the current menu is not the lowest level menu, enter the number index to enter the next level menu 46 index = int (choice) of the corresponding menu item) 47 if index> = 0 and index <len (temp_menu): 48 key = temp_menu [index] 49 upper_menu_list.append (curre Nt_menu_dict) # If the input number is included in the menu item index, add the current menu as its last element to the high-level menu list, in the current menu, enter the next menu corresponding to the index 50 current_menu_dict = current_menu_dict [key] 51 else: 52 print ('enter an error number. Please enter it again! ') 53 else: 54 print (' is already the lowest level menu, press B to return to the upper level menu, and press q to exit! ') 55 elif choice =' B ': # if the input option is "B", return to the upper menu 56 if len (upper_menu_list)> 0: # If the current menu is not the top menu, take the last element in the top menu list as the current menu Dictionary 57 current_menu_dict = upper_menu_list.pop () 58 else: # If the current menu is already the top menu, 59 print cannot be rolled back ('is already the top menu, cannot be rolled back, press the number key to select the menu item to enter the sub menu, press the q key to exit') 60 elif choice = 'q ': # If the input option is "q", exit menu 61 print ('exit menu! '. Center (50,' * ') 62 exit () 63 else: 64 print ('input error, please input again') 65 temp_menu = []
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.