首页 > 代码库 > python练习_module01-1-3级菜单_2

python练习_module01-1-3级菜单_2

3级菜单的另一种实现方式:循环

函数的方式:http://www.cnblogs.com/jailly/p/6709573.html

 

1.先创建一个能够表明3级目录结构之间映射关系的字典,然后将其存入一个pickle文件以便调用

 1 import pickle
 2 
 3 dir_windows = [explorer.exe]
 4 part_C = {windows: dir_windows}
 5 
 6 dir_setup = [rhel-7.3.iso, SecureCRT6.rar]
 7 dir_program = [putty.exe, PhotoshopCC2016.exe]
 8 part_D = {setup: dir_setup, program: dir_program}
 9 
10 dir_video = [君の名は.mp4]
11 dir_game = [NieR: Automata, 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.主程序

 1 #! /usr/bin/env python3
 2 # -*- coding:utf-8 -*-
 3 # Author:Jailly
 4 
 5 ‘‘‘
 6     以3级的目录结构模拟三级菜单,目录结构如下::
 7         C
 8             windows
 9                 explorer.exe
10         D
11             setup
12                 rhel-7.3.iso
13                 SecureCRT6.rar
14             program
15                 putty.exe
16                 PhotoshopCC2016.exe
17         E
18             video
19                 君の名は.mp4
20             game
21                 NieR: Automata
22                 Sid Meier‘s Civilization VI 
23 ‘‘‘
24 
25 import pickle
26 
27 def main(root):
28     n = 1
29     select1 = None
30     select2 = None
31     select3 = None
32 
33     while n:
34         if n == 1:
35             if not select1:
36                 print(欢迎进入%d级目录,该目录下有如下目录/文件:%s% (n,.join(sorted(root.keys()))))
37                 select1 = input(请选择您想要执行的操作:(“[目录名]”:进入目录;“q”:退出):)
38 
39             if select1 in root:
40                 n = 2
41                 continue
42             elif select1 == q:
43                 break
44             else:
45                 select1 = input(指令输入错误,请重新输入:)
46                 continue
47 
48         elif n == 2:
49             if not select2:
50                 print(欢迎进入%d级目录,该目录下有如下目录/文件:%s%(n,.join(sorted(root[select1].keys()))))
51                 select2 = input(请选择您想要执行的操作:(“[目录名]”:进入目录;“b”,返回上一级;“q”:退出):)
52 
53             if select2 in root[select1]:
54                 n = 3
55                 continue
56             elif select2 == b:
57                 n = 1
58                 continue
59             elif select2 == q:
60                 break
61             else:
62                 select2 = input(指令输入错误,请重新输入:)
63                 continue
64 
65         elif n == 3:
66             if not select3:
67                 print(欢迎进入%d级目录,该目录下有如下目录/文件:%s % (n, .join(sorted(root[select1][select2]))))
68                 select3 = input(请选择您想要执行的操作:(“b”,返回上一级;“q”:退出):)
69 
70             if select3 == b:
71                 n = 2
72                 continue
73             elif select3 == q:
74                 break
75             else:
76                 select3 = input(指令输入错误,请重新输入:)
77                 continue
78 
79 if __name__ == __main__:
80 
81     with open(dir.pkl, br) as f:
82         root = pickle.load(f)
83 
84     main(root)

 

python练习_module01-1-3级菜单_2