三级菜单打印终极版作业

程序: 三级菜单终极版

要求: 

  1. 打印省、市、县三级菜单
  2. 可返回上一级
  3. 可随时退出程序
  4. 可随时新增城市
  5. 可随时修改城市
  6. 可随时删除城市
  7. 所有操作保存到文件里

代码

  1 #Author:abu zhang
  2 import json
  3 
  4 def open_r():
  5     """
  6     读数据的函数
  7     :return:
  8     """
  9     with open("dict_data.txt",'r')  as dict_r:
 10         dict_data = json.load(dict_r)
 11         return dict_data
 12 def open_w(menu):
 13     """
 14     修改后把数据写入进来的函数
 15     :param menu:
 16     :return:
 17     """
 18     with open("dict_data.txt",'w') as dict_w:
 19         json.dump(menu,dict_w)
 20 def create(dict_data):
 21     """
 22     新建城市函数
 23     :param dict_data:传递字段记录用
 24     :return:
 25     """
 26     while True:
 27         for key in dict_data:
 28             print(key)
 29         choice2 = input("请输入你要创建的城市,按q退出创建:").strip()
 30         if choice2 == 'q':break
 31         elif len(choice2) > 0:
 32             dict_data[choice2] = {}
 33             print("你已经新建城市:\033[31;1m%s\033[;0m"%choice2)
 34         else:
 35             print("create error")
 36 def delete(dict_data):
 37     """
 38     删除数据函数
 39     :param dict_data: dict_data是当前字典信息
 40     :return:
 41     """
 42     while flag:
 43         for key in dict_data:
 44             print(key)
 45         choice3 = input("请输入你要删除的城市名,按q退出删除:").strip()
 46         if choice3 == 'q': break
 47         if len(choice3) > 0:
 48             if choice3 in dict_data:
 49                 dict_data.pop(choice3)
 50                 print("已删除\033[31;1m%s\033[;0m" % choice3)
 51             else:
 52                 continue
 53         else:
 54             print("input error")
 55 def change(dict_data):
 56     """
 57     这里是修改数据的函数
 58     :param dict_data: 传递但是数据函数
 59     :return:
 60     """
 61     while True:
 62         for key in dict_data:
 63             print(key)
 64         choice4 = input("请输入你要修改的数据,按q退出修改>>:").strip()
 65         if choice4 == 'q': break
 66         if len(choice4) > 0:
 67             if choice4 in dict_data:
 68                 dict_values = dict_data[choice4]
 69                 dict_data.pop(choice4)
 70             else:
 71                 print("\033[31;1m您要修改的城市不存在\033[;0m")
 72                 continue
 73         choice5 = input("请输入你要改成的数据:").strip()
 74         if len(choice5) > 0:
 75             dict_data[choice5] = dict_values
 76             print('您已把\033[31;1m%s\033[;0m修改为\033[32;1m%s\033[;0m' % (choice4, choice5))
 77         else:
 78             print("input error")
 79 def quit(list,dict,flag):
 80     """
 81     退出程序时,调用这个函数,函数的功能是不管你再那一层都可以退出程序,并把你修改过的数据写到文件里
 82     :param list: 传记录字典位置信息的字典进来
 83     :param dict: 传字典
 84     :param flag: flag=flasu 、true
 85     :return:
 86     """
 87     while flag:
 88         dict = list.pop()
 89         if not list:
 90             open_w(dict)
 91             flag = False
 92     return flag
 93 
 94 menu = open_r()  #开始启动程序
 95 dict_data = menu
 96 dict_list = []
 97 flag = True  #程序标志位
 98 
 99 while flag:
100     for kev in dict_data:  #循环打印字段的key
101         print(kev)
102     choice = input(">>>:").strip()  #让用户输入进入哪一层
103     if len(choice) > 0:   #判断用户输入的长度是否为空
104         if choice in dict_data:
105             dict_list.append(dict_data)  #把当前状态先保存到列表中
106             dict_data = dict_data[choice]  #重点
107         elif choice == 'create':  #
108             create(dict_data)
109         elif choice == 'del':     #
110             delete(dict_data)
111         elif choice == 'change':
112             change(dict_data)
113         elif choice == 'b':  #返回上一级
114             if dict_list:  #判断列表是否为空
115                 dict_data = dict_list.pop()  #删除返回值并赋值
116             else:
117                 open_w(dict_data)
118                 break
119         elif choice == 'q':  #退出
120             flag = quit(dict_list,dict_data,flag)  #执行退出函数,退出函数里面包含保存数据
121         else:
122             print("Input Error:")
123     else:
124         print("Input Error:")

 

作业文件,json格式

保存的是字典格式

1 {"\u5317\u4eac": {"\u6d77\u6dc0": {"\u4e94\u9053\u53e3": {"soho": {}, "\u7f51\u6613": {}, "google": {}}, "\u4e2d\u5173\u6751": {"\u7231\u5947\u827a": {}, "\u6c7d\u8f66\u4e4b\u5bb6": {}, "youku": {}}, "\u4e0a\u5730": {"\u767e\u5ea6": {}}}, "\u660c\u5e73": {"\u6c99\u6cb3": {"\u8001\u7537\u5b69": {}, "\u5317\u822a": {}}, "\u5929\u901a\u82d1": {}, "\u56de\u9f99\u89c2": {}}, "\u671d\u9633": {}, "\u4e1c\u57ce": {}}, "\u4e0a\u6d77": {"\u95f5\u884c": {"\u4eba\u6c11\u5e7f\u573a": {"\u70b8\u9e21\u5e97": {}}}, "\u95f8\u5317": {"\u706b\u8f66\u6218": {"\u643a\u7a0b": {}}}, "\u6d66\u4e1c": {}}, "\u5e7f\u4e1c": {"\u5e7f\u5dde": {"\u5929\u6cb3": {"\u71d5\u5858": {}}, "\u767d\u4e91": {}, "\u841d\u5c97": {}}, "\u6e5b\u6c5f": {"\u971e\u5c71": {"\u673a\u573a\u8def": {}, "\u7eff\u5510\u8def": {}}, "\u5f90\u95fb": {"\u66f2\u754c": {}, "\u5916\u7f57": {}, "\u524d\u5c71": {}, "\u6d4b\u8bd5": {}}, "\u8d64\u574e": {}}, "\u73e0\u6d77": {"\u5c71\u6c34": {}}, "\u6df1\u5733": {"\u5357\u6c99": {}, "\u4fdd\u5b89": {}}}}

 

posted @ 2017-09-26 09:59  zhangabu  阅读(174)  评论(0编辑  收藏  举报