Python exercise _ module01-1-3 level menu, pythonmodule
Simulate the operation of Level 3 menus with a level 3 directory.
1. First, create a dictionary that can indicate the ing between three levels of directory structures, and then store it into a pickle file for calling
1 import pickle 2 3 dir_windows = require 'cmder.exe '] 4 part_C = {'windows': dir_windows} 5 6 dir_setup = ['rhel-7.3.iso', 'SecureCRT6.rar '] 7 dir_program = require 'putty.exe ', 'PhotoshopCC2016.exe '] 8 part_D = {'setup': dir_setup, 'program': dir_program} 9 10 dir_video = 'jun Jun Ming .mp4 '] 11 dir_game = ['nier: A ', 'sid Meier \'s Civilization VI '] 12 part_E = {'video': dir_video, 'game': dir_game} 13 14 root = {'C': part_C, 'D': part_D, 'E': part_E} 15 16 with open ('dir. pkl ', 'wb') as f: 17 pickle. dump (root, f)
2. Main Program
1 #! /Usr/bin/env python3 2 #-*-coding: UTF-8-*-3 # Author: Jailly 4 5 ''' 6 simulate Level 3 menus with a level 3 directory structure, the directory structure is as follows: 7 C 8 windows 9 explorer.exe 10 D11 setup12 rhel-7.3.iso13 SecureCRT6.rar14 program15 putty.exe 16 PhotoshopCC2016.exe17 E18 video19 は.mp4 20 game21 NieR: automata22 Sid Meier's Civilization VI 23 ''' 24 25 import time, OS, pickle26 27 def quit (): 28 print ('Thank you for your use, exiting the system ', end = '') 29 for I in range (3): 30 Time. sleep (0.5) 31 print ('.', end = '') 32 time. sleep (0.5) 33 print ('\ n has exited! ') 34 time. sleep (1) 35 exit () 36 37 def s1 (select1 = None): 38 if not select1: 39 print ('Welcome to the system. The root directory contains the following partitions: % s '% (','. join (sorted (root. keys () 40 select1 = input ('select the operation you want to perform: ("[partition name]": Enter the partition; "q": Exit ):') 41 42 if select1 in root. keys (): 43 s2 (select1) 44 elif select1 = 'q': 45 quit () 46 else: 47 select1 = input ('your input is incorrect, enter: ') 48 s1 (select1) 49 50 def s2 (select1, select2 = None): 51 52 if not select2: 53 print ('current path is % s, the following directories are available: % s' % (select1 ,','. join (sorted (root [select1]. keys () 54 select2 = input ('select the operation you want to perform ("[directory name]": Enter the Directory; "B": Return to the upper level; "q": Exit): ') 55 56 if select2 in root [select1]. keys (): 57 s3 (select1, select2) 58 elif select2 = 'B': 59 s1 () 60 elif select2 = 'q': 61 quit () 62 else: 63 select2 = input ('your input is incorrect, please enter again: ') 64 s2 (select1, select2) 65 66 def s3 (select1, select2, select3 = None ): 67 68 if not select3: 69 print ('current path is % s, with the following file: % s' % (OS. path. join (select1, select2 ),','. join (sorted (root [select1] [select2]) 70 select3 = input ('select the operation you want to perform ("B": Return to the upper level; "q ": exit): ') 71 72 if select3 =' B ': 73 s2 (select1) 74 elif select3 = 'q': 75 quit () 76 else: 77 select3 = input ('your input is incorrect. Please enter it again ("B": return the upper level, "q": Exit): ') 78 s3 (select1, select2) 79 80 if _ name _ = '_ main _': 81 82 with open ('dir. pkl ', 'br') as f: 83 root = pickle. load (f) 84 85 s1 ()
Or use a loop to achieve: http://www.cnblogs.com/jailly/p/6710179.html