python中用字典实现三级菜单

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

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 loop
indicater1 = True
province_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直接在程序中给出,也可以保存在一个文本文件或者数据库汇总。可在此程序基础上扩展。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值