python练习_module01-1-三级菜单
以3级目录来模拟3级菜单的运作。
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 time,os,pickle 26 27 def quit(): 28 print('谢谢您的使用,正在退出系统',end = '') 29 for i in range(3): 30 time.sleep(0.5) 31 print('.',end = '') 32 time.sleep(0.5) 33 print('\n已退出!') 34 time.sleep(1) 35 exit() 36 37 def s1(select1 = None): 38 if not select1: 39 print('欢迎进入系统,根目录下有如下分区:%s' % ('、'.join(sorted(root.keys())))) 40 select1 = input('请选择您想要执行的操作:(“[分区名]”:进入分区;“q”:退出):') 41 42 if select1 in root.keys(): 43 s2(select1) 44 elif select1 == 'q': 45 quit() 46 else: 47 select1 = input('您的输入不正确,请重新输入:') 48 s1(select1) 49 50 def s2(select1,select2 = None): 51 52 if not select2: 53 print('当前路径为%s,有如下目录:%s' % (select1,'、'.join(sorted(root[select1].keys())))) 54 select2 = input('请选择您要执行的操作(“[目录名]”:进入目录;"b":返回上一级;“q”:退出):') 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('您的输入不正确,请重新输入:') 64 s2(select1,select2) 65 66 def s3(select1,select2,select3 = None): 67 68 if not select3: 69 print('当前路径为%s,有如下文件:%s' % (os.path.join(select1,select2),'、'.join(sorted(root[select1][select2])))) 70 select3 = input('请选择您要执行的操作(“b”:返回上一级;“q”:退出):') 71 72 if select3 == 'b': 73 s2(select1) 74 elif select3 == 'q': 75 quit() 76 else: 77 select3 = input('您的输入不正确,请重新输入(“b”:返回上一级,“q”:退出):') 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()
或者使用循环的方式来实现:http://www.cnblogs.com/jailly/p/6710179.html