三级菜单(1)

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

# -*- 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 os
city = {"北京市":{"北京市":["东城区","西城区","朝阳区","丰台区"]
                },
        "天津市":{"天津市":["和平区","河东区","河西区","南开区","河北区"],
                "经开区":["经开西区","经开东区"]
                }
        }
sheng = sorted(list(city.keys()))
back_flag = False
exit_flag = False
while 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))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值