Python program Practice 2--simulation Level Three menu

Source: Internet
Author: User

1. Function Introduction

This program simulates multi-level menu operation, the implementation of a menu item corresponding to the digital index into the sub-menu, press the B key back to the previous menu, press the Q key to exit the menu. A three-level menu test is performed with a simplified discipline catalog, which can be used in virtually any multilevel menu operation.

2. Implementation methods

This program is written in Python language, in order to effectively implement menu operation, reduce the number of cycles, the key defines the following three variables:

    • current_menu_dict: a nested dictionary when the menu is not the lowest level, a list at the bottom of the menu, and a menu for the current level and subsequent levels.
    • upper_menu_list: A list that holds a list of all the menu nested dictionaries that are higher than the current level, and the last element is a parent menu more than Current_menu_dict.
    • temp_menu: A list that stores only the current level of the menu, which is current_menu_dict the key value.

Menu Operation Process:
(1) Press the number key to enter the corresponding sub-menu: The number key is temp_menu the index, according to the index to extract the current menu item, that is, current_menu_dict a key value, which current_menu_dict can be re-pointed to the key value corresponding to the next-level menu nested dictionary, but before entering the sub-menu, upper_menu_list.append(current_menu_dict) The current menu nested dictionary is added as upper_menu_list the last element to prepare for fallback.
(2) Press the B key to fall back to the previous level menu: fallback when you current_menu_dict=upper_menu_list.pop() get a previous menu nested dictionary to achieve fallback.
(3) Press the Q key to exit the menu: Press the Q key when the exit() exit program is called.

Simple example: Suppose the Level three menu dictionary is {一级菜单:{二级菜单:三级菜单}} , if the current menu is in the second level, this time
current_menu_dict={二级菜单:三级菜单}
upper_menu_list=[{一级菜单:{二级菜单:三级菜单}}]
temp_menu=[二级菜单]

3. Flowchart

4. Code
1 #Author:byron Li2 3Menu_data={4     'Literature':5         {6             'Chinese language and literature':['Linguistics and Applied Linguistics','A study of Chinese language and philology','Ancient Chinese literature','Contemporary Chinese Literature'],7             'foreign language and literature':['English language and literature','Asian-African language and literature','Arabic language and literature','foreign linguistics and Applied Linguistics'],8             'Journalism and Communication': ['Journalistic Science','Communication'],9             'Art Studies': ['Music Science','Art Studies','drama and Drama Science','Film Studies','Dance Studies']Ten         }, One  A     'Rigaku': -         { -             'Mathematics':['Basic Mathematics','Computational Mathematics','probability theory and mathematical statistics','Applied Mathematics','Operational Research and Cybernetics'], the             'Physics':['Theoretical Physics','particle physics and nuclear physics','Atomic and Molecular physics','Plasma Physics','Radio Physics'], -             'Chemical': ['Inorganic Chemistry','Analytical Chemistry','Organic Chemistry','Physical Chemistry','Polymer Chemistry and Physics'], -             'Biology': ['Botany','Zoology','Physiology','Microbiology','Genetics'], -             'Geophysical Sciences': ['Solid Earth Physics','Space Physics'] +         }, -     'Engineering': +         { A             'Mechanical Engineering':['Machinery Manufacturing and its automation','Mechanical and Electronic engineering','mechanical Design and theory','Vehicle Engineering'], at             'Electrical Engineering':['power system and its automation','high Voltage and insulation technology','power electronics and power transmission','Electrician theory and new technology'], -             'Computer Science and technology': ['computer system Structure','computer software and theory','Computer Application Technology'], -             'Civil Engineering': ['Geotechnical Engineering','Structural Engineering','Municipal Engineering','Disaster prevention and mitigation engineering and protection engineering','Bridge and tunnel engineering'], -             'Geological resources and geological engineering': ['Mineral Survey and exploration','Earth Exploration and information technology','Geological Engineering'] -         } - } in  -Current_menu_dict=menu_data#a nested dictionary that holds the current level and subsequent levels of menus, at the lowest level, as a list toUpper_menu_list=[]#holds a list of all menu dictionaries that are higher than the current level, and the last element is more than current_menu_dict a parent menu +Temp_menu=[]#A temporary list that holds the current level menu -  the  while(True): *      forI,subjectinchEnumerate (current_menu_dict):#loop to display the index and menu items for the current menus $         ifType (current_menu_dict) = = Dict:#If the current menu is not the lowest level menu, the Index and menu items are displayedPanax Notoginseng            Print("'. Join (['\ t'*len (Upper_menu_list),'[', str (i),']', subject])) -  the         Else:#If the current menu is the lowest level menu, only the menu item is displayed, the index is not displayed +             Print("'. Join (['\ t'*Len (upper_menu_list), subject])) A temp_menu.append (subject) the  +Choice=input ("'. Join (['\ t'*len (Upper_menu_list),'>>>']))#Input Options -     ifChoice.isdigit ():#If the input option is a numeric index corresponding to the menu item, enter the menu item below the level menu $         ifType (current_menu_dict) = = Dict:#If the current menu is not the lowest level menu, enter a numeric index to enter the next level menu for the corresponding menu item $index =int (choice) -             ifIndex >= 0 andIndex <Len (temp_menu): -Key =Temp_menu[index] theUpper_menu_list.append (current_menu_dict)#If the input number is included in the menu item index, the High-level menu list adds the current menu to its last element, while the current menu enters the next-level menu for the index -Current_menu_dict =Current_menu_dict[key]Wuyi             Else: the                 Print('Enter the wrong number, please re-enter! ') -         Else: Wu             Print('is the lowest level menu, press the B key to return to the parent menu, press the Q key to exit!') -     elifchoice=='b':#If the input option is "B", return to the previous menu About         ifLen (upper_menu_list) > 0:#If the current menu is not a top level menu, take the last element in the high levels menu list as the current menu dictionary $current_menu_dict=Upper_menu_list.pop () -         Else:#cannot fallback if the current menu is already a top-level menu -             Print('is already the top-level menu, cannot be rolled back, press the number key to select the menu item to enter the submenu, press the Q key to exit') -     elifchoice=='Q':#If the input option is "Q", exit the menu A         Print('Exit the Menu! '. Center (50,'*')) + exit () the     Else: -         Print('input Error, please re-enter') $Temp_menu=[]
View Code

Python program Practice 2--simulation Level Three menu

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.