三级菜单(1)

来源:互联网 发布:淘宝店铺手机怎么激活 编辑:程序博客网 时间:2024/06/01 10:44

要求:省市县三级菜单查询,可返回上级,可任意级退出

# -*- coding: utf-8 -*-# @Author: oppend# @Date:   2017-07-27 23:10:46# @Last Modified by:   oppend# @Last Modified time: 2017-07-29 23:26:57# 省市区三级菜单import oscity = {"北京市":{"北京市":["东城区","西城区","朝阳区","丰台区"]                },        "天津市":{"天津市":["和平区","河东区","河西区","南开区","河北区"],                "经开区":["经开西区","经开东区"]                }        }sheng = sorted(list(city.keys()))back_flag = Falseexit_flag = Falsewhile not back_flag and not exit_flag:    # 输出省列表    print('-----省-----')    for snum,sname in enumerate(sheng,1):        print('[{0}]{1}'.format(snum,sname))    choice_sheng = input('省序号[q]退出:')    os.system('clear')    # 判断是否为数字    if choice_sheng.isdigit():        choice_sheng = int(choice_sheng)-1        # 判断省序号范围        if choice_sheng >= len(sheng) or choice_sheng < 0:            print('序号{0}错误'.format(choice_sheng+1))            continue        # 根据序号取省名称        while not back_flag and not exit_flag:            sheng_name = sheng[choice_sheng]            shi = sorted(list(city[sheng_name].keys()))            # 输出市列表            print('-----市-----')            for shi_num,shi_name in enumerate(shi,1):                print('[{0}]{1}'.format(shi_num,shi_name))            choice_shi = input('市序号[u]回上级[q]退出:')            os.system('clear')            if choice_shi.isdigit():                choice_shi = int(choice_shi)-1                # 判断市序号范围                if choice_shi >= len(shi) or choice_shi < 0:                    print('序号{0}错误'.format(choice_shi+1))                    continue                # 根据序号取市名称                while True:                    shi_name = shi[choice_shi]                    xian_list = city[sheng_name][shi_name]                    # 输出县列表                    print('-----县-----')                    for xian_num,xian_name in enumerate(xian_list,1):                        print('[{0}]{1}'.format(xian_num,xian_name))                    choice_xian = input('[u]回上级[q]退出:')                    os.system('clear')                    if not choice_xian.isdigit():                        if choice_xian.lower() == 'u':                            break                        elif choice_xian.lower() == 'q':                            back_flag = True                            exit_flag = True                            break                    else:                        print('命令错误!')            else:                if choice_shi.lower() == 'u':                    break                elif choice_shi.lower() == 'q':                    back_flag = True                    exit_flag = True                    break                else:                    print('序号{0}不存在!'.format(choice_shi))    else:        if choice_sheng.lower() == 'q':            break        else:            print('序号{0}不存在!'.format(choice_sheng))
原创粉丝点击