zoukankan      html  css  js  c++  java
  • 多级菜单(增强版)

     1 #__Author__ oliver
     2 #__Date__ 2016/8/26
     3 
     4 
     5 with open('省市县','r',encoding='utf-8') as f_read:
     6     data = f_read.readlines()   #将文件内容放入大列表
     7     # data = f_read.read()      #直接读出字符串
     8 zone = eval(''.join(data))     #获取地区字典
     9 current_layer = dict(zone)
    10 parent_layer = []
    11 while True:
    12     for key in current_layer:
    13         print(key)
    14     choice = input("Press <a> Add,press <m> Modify,press <d> Delete;press <b> back,press <q> Quit.
    >>>:").strip()
    15     if len(choice) == 0:
    16         continue
    17     if choice in current_layer:
    18         parent_layer.append(current_layer)
    19         current_layer = current_layer[choice]
    20         if current_layer == {}:
    21             print("已到最后一级。")
    22     elif choice == 'b':
    23         if parent_layer:
    24             current_layer = parent_layer.pop()
    25     elif choice == 'q':
    26         while parent_layer != []:
    27             current_layer = parent_layer.pop()
    28         break
    29     elif choice == 'a': # add操作
    30         new_item = input("请输入新地区>>>").strip()
    31         if new_item in current_layer:
    32             print("地区%s已存在,请重新输入。
    "%new_item)
    33         else:
    34             new_dic = {}
    35             current_layer.setdefault(new_item,new_dic) #增加菜单
    36     elif choice == 'd':
    37         del_item = input("请输入删除项>>>")
    38         if del_item in current_layer:
    39             del current_layer[del_item]              #删除菜单
    40         else:
    41             print("菜单中没有'%s'地区!
    "%del_item)
    42     elif choice == 'm':
    43         modify_item = input("请输入修改项>>>").strip()
    44         save_value = current_layer[modify_item]    #保存用户输入的key对应的value
    45         modify_key = input("请输入新地区>>>").strip()
    46         del current_layer[modify_item]
    47         modify_dic = {modify_key : save_value}      #新键值对
    48         current_layer.update(modify_dic)            #将新的键值对添加到字典中
    49     else:
    50         print("无此项!
    ")
    51 with open('省市县_new','w',encoding='utf-8') as f_write:
    52     f_write.write(str(current_layer))
  • 相关阅读:
    mysql索引最左匹配的理解(转载于知乎回答)
    mysql深度优化与理解(迄今为止读到最优秀的mysql博客)
    PHP数组函数总结与使用
    进程(process)和线程(thread)
    联合索引使用规则(转载)
    mysql优化大全(转自别人 )
    HTTP隧道解决的问题
    HTTP代理协议 HTTP/1.1的CONNECT方法
    vant弹窗提示
    vue获取验证码倒计时
  • 原文地址:https://www.cnblogs.com/pyramid1001/p/5814940.html
Copyright © 2011-2022 走看看