python小程序(模拟三级菜单)

需求:

(实现三级菜单的依次递归查询,输入一级菜单某项,依次列出下级菜单内容)

下图为程序流程图

代码实现

 1 #!/usr/bin/env python
 2 #-*- coding=utf-8 -*-
 3 #author:sunwei
 4 import os
 5 import time
 6 players_info = {
 7     "美国东部":{
 8         "热火":["韦德","波什","德拉季奇"],
 9         "骑士":["詹姆斯","欧文","乐福"],
10         "公牛":["洛尔","乔丹","罗斯"]
11     },
12     "美国中部":{
13         "篮网":["易建联","德隆","乔约翰逊"],
14         "凯尔特人":["雷阿伦","加内特","皮尔斯"],
15         "公牛":["艾佛森","马龙","J博士"]
16     },
17     "美国西部":{
18         "勇士":["库里","格林","杜兰特"],
19         "火箭":["姚明","麦迪","弗朗西斯"],
20         "马刺":["邓肯","吉诺比利","帕克"]
21     }
22 }
23 print("欢迎来到NBA球员检索系统!!!")
24 time.sleep(1)
25 os.system("clear")
26 
27 
28 while True:
29     for area in players_info:
30         print(area)  #打印第一层目录
31     area_input = input("\033[1;32;40m请输入你想要查询的地区(q退出):\033[0m").strip()
32     if len(area_input) == 0:continue  #用户输入为空则跳出本次循环,继续下次循环
33     if area_input in players_info:    
34         teamlist = players_info[area_input]
35         while True:
36         #    print(teamlist)  输出为一个某一地区的value值,表现形式为字典
37             for team in teamlist:
38                 print(team)  #打印第二层目录
39             team_input = input("\033[1;32;40m请输入你想要查询的球队(q退出,b返回上一层):\033[0m").strip()
40             if len(team_input) == 0:continue
41             if team_input in teamlist:
42                 while True:
43                     for players in teamlist[team_input]:
44                         print(players)#最后一层的数据类型为列表,直接打印
45                     user_input = input("\033[1;32;40mq退出,b返回上一层):\033[0m").strip()
46                     if len(user_input) == 0:continue
47                     if user_input == "q":
48                         exit()
49                     elif user_input == "b":
50                         break
51                     else:
52                         print("\033[1;31;40m输入有误,请重试!!!\033[0m")
53             elif team_input == "q":
54                 exit()
55             elif team_input == "b":
56                 break
57             else:
58                 print("\033[1;31;40m输入有误,请重试!!!\033[0m")    
59     elif area_input == "q":
60         exit()
61     else:
62         print("\033[1;31;40m输入有误,请重试!!!\033[0m")
three_level_menu.py

上述代码过于繁琐,后期会精简代码............

经过进一步的学习后,掌握了python的文件操作,于是.....

1.文件如下

编程语言:    默认全部折叠  显示行号

{
    "美国东部":{
        "热火":{
            "韦德":["后卫"],
            "波什":["后卫"],
            "德拉季奇":["前锋"]
        },    
        "骑士":{
            "詹姆斯":["前锋"],
            "欧文":["后卫"],
            "汤普森":["中锋"]    
        },
        "公牛":{
            "罗斯":["后卫"],
            "诺阿":["中锋"],
            "罗尔邓":["前锋"],
        }
    },
    "美国西部":{
        "勇士":{
            "库里":["后卫"],
            "博古特":["后卫"],
            "杜兰特":["前锋"]
        },    
        "火箭":{
            "麦迪":["前锋"],
            "阿尔斯通":["后卫"],
            "姚明":["中锋"]
        },
        "马刺":{
            "帕克":["后卫"],
            "邓肯":["中锋"],
            "吉诺比利":["前锋"],
        }
    }
}
换一种代码着色方法
确定  关闭
info.txt

2.代码如下

#!/usr/bin/env python
#-*- coding=utf-8 -*-
#author:sunwei
import time
exit_flag = False
while not exit_flag:
    f =  open("info.txt","r",encoding="utf-8") 
    f1 = eval(f.read())
    for i1 in f1:
        print("\033[0;31;40m%s\033[0m"%i1)
    choice1 = input("选择进入一级菜单[q退出]>>")
    if choice1 in f1:
        while not exit_flag:
            for i2 in f1[choice1]:
                print("\t",i2)
            choice2 = input("选择进入二级菜单[q退出,b返回上一级]>>")
            if choice2 in f1[choice1]:
                while not exit_flag:
                    for i3 in f1[choice1][choice2]:
                        print("\t\t",i3)
                    choice3 = input("选择进入三级菜单[q退出,b返回上一级]>>")
                    if choice3 in f1[choice1][choice2]:
                        for i4 in f1[choice1][choice2][choice3]:
                            print("\t\t\t",i4)
                        while not exit_flag:
                            choice4 = input("已经是最后一层,输入[q退出,b返回上一级]>>")
                            if choice4 == "b":
                                break
                            elif choice4 == "q":
                                print("即将离开系统")
                                time.sleep(2)
                                exit_flag = True
                                    
                    elif choice3 == "b":
                        break
                    elif choice3 == "q":
                        print("即将离开系统")
                        time.sleep(2)
                        exit_flag = True
            elif choice2 == "b":
                break
            elif choice2 == "q":
                exit_flag = True
                print("即将离开系统")
                time.sleep(2)
    elif choice1 == "q":
        exit_flag = True
        print("即将离开系统")
        time.sleep(2)
f.close()
three_level_menu.py

3.执行

[root@localhost three_level_menu]# python three_level_menu.py 
美国东部
美国西部
选择进入一级菜单[q退出]>>美国西部
	 勇士
	 火箭
	 马刺
选择进入二级菜单[q退出,b返回上一级]>>勇士
		 库里
		 博古特
		 杜兰特
选择进入三级菜单[q退出,b返回上一级]>>库里
			 后卫
已经是最后一层,输入[q退出,b返回上一级]>>q
即将离开系统

未完待续.........(利用一层循环达到目的)

posted @ 2017-09-11 22:09  幸福的好喜欢  阅读(513)  评论(0编辑  收藏  举报