首页 > 代码库 > Multi-level menu

Multi-level menu

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import sys
#全局列表
dic_with_num = {}
dic_with_chinese = {}

#字典导入
def dic_import ():
    with open(dic.txt,r+) as file:
        file_list = file.readlines()
        dic= {}
        dic1 = {}
        dic2 = {}
        dic3 = {}
        i = 1
        j = 1
        k = 1
        for line in file_list :
            (key1,key2) = line.strip(\n).split()
            if key2 == :
                i = 1
                j = 1
                line1 = key1
                dic1[line1]=dic3
                dic3 = {}
                dic2[str(k)] = dic1
                dic1 = {}
                dic = dict(dic,**dic2)
                dic2 ={}
                k +=1


            elif key2 == :
                i = 1
                line2 = key1
                dic2[line2]=dic1
                dic1={}
                dic1[str(j)]=dic2
                dic2={}
                dic3=dict(dic3,**dic1)
                dic1={}
                j +=1

            else:
                line3 = key1
                dic1[str(i)] = line3
                i +=1
    return dic

#字典交换-当用户输入中文时,转换字典
def dic_exchange ():
    dic_with_chinese = {}
    dic = {}
    dic1 = {}
    for line1 in dic_with_num.keys():
        for line2 in dic_with_num[line1].keys():

            for line3 in dic_with_num[line1][line2].keys():
                for line4 in dic_with_num[line1][line2][line3].keys():

                    for line5,line6 in dic_with_num[line1][line2][line3][line4].items():
                        dic[line6] = line5
                    dic1[line3] = dic
                    dic = {}
                    dic[line4] = dic1
                    dic_with_chinese = dict(dic_with_chinese,**dic)
                    dic = {}
                    dic1 ={}
            dic[line1] = dic_with_chinese
            dic_with_chinese = {}
            dic_with_chinese[line2] = dic
            dic1 = dict(dic1,**dic_with_chinese)
            dic = {}
            dic_with_chinese = {}
    return dic1




#初始化
dic_with_num = dic_import()
dic_with_chinese = dic_exchange()
while True:
    #省份显示
    for k in dic_with_num.keys():
        for i in dic_with_num[k].keys():
            print(k,i + \t, end= ‘‘)
            if  int(k)%4 ==0:
                print(\n)
    #询问用户需要查询的省份,可输入中文
    province = input(\n请输入需要查询的省份\n).strip()

    #城市显示

    #用户输入为序号时

    if province.isdigit():
        if province not in dic_with_num.keys():
            print(选项不存在\n)
            continue
        else:
            province_chinese = list(dic_with_num[province].keys())[0] #获取序号对应的省份中文名
            for k in dic_with_num[province][province_chinese].keys():
                for i in dic_with_num[province][province_chinese][k].keys():
                    print(k,i + \t ,end = ‘‘)
                    if int(k) % 4 == 0:
                        print(\n)
    #用户输入中文
    else:
        if province not in dic_with_chinese.keys():
            print(选项不存在\n)
            continue
        else:
            province_chinese = list(dic_with_chinese[province].keys())[0] #获取省份对应的序号
            for k in dic_with_chinese[province][province_chinese].keys():
                for i in dic_with_chinese[province][province_chinese][k].keys():
                    print(i,k + \t,end=‘‘)
                    if int(i) % 4 == 0:
                        print(\n)


    #询问用户需要查询的城市,可输入中文
    while True:
        city = input(请输入需要查询的城市\n)
        #用户输入为序号时
        if city.isdigit():
            if city not in dic_with_num[province][province_chinese].keys():
                print(选项不存在\n)
                continue
            else:
                city_chinese = list(dic_with_num[province][province_chinese][city].keys())[0]#获取序号对应的城市中文名
                for k ,v in dic_with_num[province][province_chinese][city][city_chinese].items():
                    print(k,v + \t,end=‘‘)
                    if int(k) % 4 == 0:
                        print(\n)
                choice = input(\n可输入q返回上一层菜单\n输入m返回主菜单\n输入其他则退出该系统)
                if choice == q:
                    province_chinese = list(dic_with_num[province].keys())[0]  # 获取序号对应的省份中文名
                    for k in dic_with_num[province][province_chinese].keys():
                        for i in dic_with_num[province][province_chinese][k].keys():
                            print(k, i + \t ,end=‘‘)
                            if int(k) % 4 == 0:
                                print(\n)
                    continue
                elif choice == m:
                    break
                else:
                    sys.exit()

        #用户输入q时 返回省份显示菜单
        elif city == q:
            break
        #用户输入为中文时
        else:
            if city not in dic_with_chinese[province][province_chinese].keys():
                print(选项不存在\n)
                continue
            else:
                city_chinese = list(dic_with_chinese[province][province_chinese][city].keys())[0]  # 获取序号对应的城市中文名
                for k, v in dic_with_chinese[province][province_chinese][city][city_chinese].items():
                    print(v, k)
                choice = input(‘‘‘
                =======================
                **可输入q返回上一层菜单**
                  >输入m返回主菜单<
                >输入其他则退出该系统<
                =======================
                ‘‘‘)
                if choice == q:
                    province_chinese = list(dic_with_chinese[province].keys())[0]  # 获取省份对应的序号
                    for k in dic_with_chinese[province][province_chinese].keys():
                        for i in dic_with_chinese[province][province_chinese][k].keys():
                            print(i, k)
                    continue
                elif choice == m:
                    break
                else:
                    sys.exit()

 

Multi-level menu