Python day 4, quit smoking day 4

Source: Internet
Author: User

Python day 4, quit smoking day 4

Today is a hurdle, and it gets stuck when making a level-3 menu. For concise code, recursive functions are required! However, with the help of the students and teachers, the solution was solved!

Job Requirements:

Level 3 menu:
1. Run the program to output the first menu
2. Select a level-1 menu and output level-2 menus. Similarly, level-3 menus are output.
3. Save menu data in a file

File operations have not yet been learned. For the time being, do the following:, and add the information the day after tomorrow!

As usual, first go to the flowchart:

Code details:

1 #! Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # Author calmyan 4 # define dictionary menu layer-3 5 info = {6 "": {7 "refrigerator": {8 "double doors": ['haier ', 'siemens', 'rongsheng'], 9 "Multiple doors": ['haier ', 'rongsheng ', 'hisense '], 10 "three doors": ['bosch', 'hisense ', 'midea'], 11 "Open Door": ['midea ', 'samsung ', 'Volume sound'] 12}, 13 "washing machine": {14 "mini": ['little Swan ', 'haier', 'panasonic '], 15 "drum ": ['sanyo ', 'Swan', 'rongsheng'], 16 "po": ['tcl ', 'mi', 'lg'], 17 "one ": ['midea ', 'lg', 'siemens '] 18}, 19 "air conditioners": {20 "air conditioning and cooling": [], 21 "central air conditioning": [], 22 "cabinet type": [], 23 "wall-mounted": [] 24} 25 }, 26 "computer": 27 {28 "whole machine": {29 "Notebook": [], 30 "desktop": [], 31 "All-in-One Machine": [], 32 "server": [] 33}, 34 "peripherals": {35 "keyboard": [], 36 "Mouse": [], 37 "camera": [], 38 "U disk": [] 39}, 40 "accessories": {41 "CPU": [], 42 "Memory": [], 43 "video card": [], 44 "main board": [] 45} 46 47}, 48 "books": {49 "humanities": {50 "History": [], 51 "Psychology ": [], 52 "Chinese Studies": [], 53 "law": [] 54}, 55 "children": {56 "Picture Book": [], 57 "Popular Science ": [], 58 "Intelligence Development": [], 59 "manual games": [] 60}, 61 'literature and art ': {62 'fiction': [], 63 'literature ': [], 64' Biography ': [], 65 'calligraphy': [] 66} 67} 68} 69 70 71 72 def menu_open (info): # recursive function incoming next layer menu 73 global exit_flag, conut # declare the global variable 74 # conut = 1 75 exit_flag = True # first True 76 while exit_flag: 77 if conut <4: # judge whether the menu level is not the bottom layer and add 78 conut + = 1 79 _ tab2 = _ tab. expandtabs (tabsize = (conut * 3) # To increase the readability of the menu, add the tab key 80 for I in info: 81 print (_ tab2, I) by level) 82 if conut = 4: # if the level is 4, it is also the last layer of 83 name = input ("currently 4th is the last layer returned by B/B <exit by Q/q:") 84 else: 85 nam E = input ("current is layer \ 033 [32; 1 m % s \ 033 [0 m, enter a name to go to the next layer> return by B/B <exit by Q/q: "% conut) 86 if name = 'B' or name = 'B': 87 conut-= 2 # Add one after repeating the factor, so we need to subtract 2 88 break 89 if name = 'q' or name = 'q': 90 print ("You have exited the menu! ") 91 exit_flag = False 92 break 93 if name in info and conut! = 4: # If the last layer does not exist, call the 94 menu_open (info [name]) 95 else: 96 conut-= 1 97 print ("the directory you entered does not exist! ") 98 continue 99 100 101 global conut, _ tab102 conut = 1103 _ tab = '\ t \ t' 104 105 exit_flag = True106 while exit_flag: 107 for I in info: 108 print (I) 109 name = input ("current is the \ 033 [32; 1 m 1 \ 033 [0 m layer, enter a name to go to the next layer >>> press Q/q to exit: ") 110 if name = 'q' or name = 'q ': 111 print ("You have exited the menu! ") 112 exit_flag = False113 114 if name in info: 115 menu_open (info [name]) 116 else: 117 print (" the directory you entered does not exist! ") 118 continue
View Code

The menu content is relatively small. Let's just move it on. Let's get it full again later!

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.