python中用字典实现三级菜单

来源:互联网 发布:互联网软件开发工资 编辑:程序博客网 时间:2024/05/21 09:20

下面的代码为用字典数据类型实现一个三级菜单查找的功能,基本操作如下:

1. 菜单分为:省—市—县(区)三级。

2. 在任何一级的时候,用户都可以通过输入‘q’或者‘Q’结束(quit)程序。

3. 在省这一级的时候用户可以通过数据省的编号进入下一级菜单,输入无效字符会出现提示,并留在当前菜单。

4. 在市这一级的时候用户可以通过输入市的编号查看下属县(区),或者输入‘u’返回上一级(省)菜单。

输入无效字符会出现提示并留在当前菜单。

5. 在县这一级菜单的时候用户可以通过输入‘u’返回上一级(市),输入无效字符会出现提示。

# Author:Allen Liu# Data: 07/26/2017'''This program is a three-stage list which use dictionary realized'''data_dic = {'山东': {'菏泽': ['郓城', '曹县'], '济南': ['历城区', '长青区']},\         '江苏': {'苏州': ['沧浪','相城'], '南京': ['白下', '秦淮']}}# you can use this character to separate a line# use indicater to end the while loopindicater1 = Trueprovince_list = list(data_dic.keys())while indicater1:    print(" Province ".center(50, '*'))    for i in province_list:        print(province_list.index(i)+1,': ',i)    print('Attention: you can input \q or \Q to stop the program!')    province_number = input('Please input the province number you want to request:')    if province_number.isdigit():# judge the type of the input        if int(province_number)>0 and int(province_number)<=len(province_list):            indicater2 = True            while indicater2:                province_request = province_list[int(province_number)-1]                print('The city of \'%s\' include:'.center(50, '*') % province_request)                city_list = list(data_dic[province_request])                for j in city_list:                    print(city_list.index(j)+1,': ',j)                city_number = input('You can input the number of the city to see the county or input \'u\' back to upper level: ')                if city_number.isdigit():                    if int(city_number)>0 and int(city_number)<=len(city_list):                        indicater3 = True                        while indicater3:                            city_request = city_list[int(city_number)-1]                            print('The county of \'%s\' include:'.center(50, '*') % city_request)                            county_list = list(data_dic[province_request][city_request])                            for k in county_list:                                print(county_list.index(k) + 1, ': ', k)                            county_number = input('You can input \'u\' back to upper or just stop this level:')                            while indicater3:                                if county_number == 'u':                                    indicater3 = False                                elif county_number.lower() == 'q':                                    indicater1,indicater2,indicater3 = False,False,False                                else:                                    print('Your input is illegal, please input again:')                                    break                    else:                        print('The number is too small or large, please input again:')                elif city_number == 'u':                    break                elif city_number.lower() == 'q':                    print('You stopped the program!')                    indicater1,indicater2 = False, False                else:                    print('The input is not integer or \'u\' or \'q\', please input again:')                    continue        else:            print('The number is too small or large, please input again:')    else:        if province_number.lower() == 'q':            print('You stopped the program!')            indicater1 = False        else:            print('The input is not integer or \'q\', please input again: ')
以上的菜单数据data_dic直接在程序中给出,也可以保存在一个文本文件或者数据库汇总。可在此程序基础上扩展。


原创粉丝点击