python3--尝试写一个三级菜单

发布时间:2018-03-26 11:34:37编辑:Run阅读(3520)

    # 程序: 三级菜单

    # 要求:

    # 1.打印省、市、县三级菜单

    # 2.可返回上一级

    # 3.可随时退出程序,可返回顶部重新开始程序


    代码如下

    #!/usr/bin/env python
    # coding: utf-8
    __author__ = 'www.py3study.com'
    class city_info(object):
        def __init__(self):
            self.list = {
                        '湖北省': {
                            '武汉市': ['新洲', '武昌', '汉口', '汉阳'],
                            '黄冈市': ['麻城', '黄陂', '罗田', '英山'],
                            '咸宁市': ['通城', '通山', '嘉鱼', '崇阳'],
                            '孝感市': ['汉川', '应城', '云梦', '安陆'],
                        },
                        '湖南省': {
                            '郴州市': ['湖区', '苏仙', '贵阳', '桂东'],
                            '衡阳市': ['珠晖', '石鼓', '常宁', '衡山'],
                            '长沙市': ['芙蓉', '天心', '开福', '雨花'],
                            '湘潭市': ['雨湖', '岳塘', '韶山', '湘乡'],
                        },
                        '广东省': {
                            '广州市': ['越秀', '东山', '天河', '白云'],
                            '惠州市': ['龙门', '博罗', '惠城', '惠阳'],
                            '东莞市': ['莞城', '东城', '南城', '万江'],
                            '佛山市': ['南海', '顺德', '高明', '三水'],
                        },
                        '广西省': {
                            '南宁市': ['江南', '良庆', '武鸣', '青秀'],
                            '桂林市': ['秀峰', '叠彩', '象山', '雁山'],
                            '玉林市': ['北流', '陆川', '玉州', '福绵'],
                            '柳州市': ['柳南', '鱼峰', '城中', '柳北'],
                        }
            }
            self.run()
    
        def run(self):
            while True:
                content = input("是否打印省列表:\nY/y(yes/Yes)打印省列表\nQ/q(quit/QUIT)退出程序\
                \n输入你的选择:").strip()
                if content.upper() == 'Y' or content.upper() == 'YES':
                    self.sheng()
                    return True
                elif content.upper() == 'Q' or content.upper() == 'QUIT':
                    exit()
                else:
                    print("输入有误!")
    
        def sheng(self):
            while True:
                self.sheng_index = list(self.list.keys())
                for i in self.sheng_index:
                    print("\t{}\t{}\t".format(self.sheng_index.index(i)+1, i))
                self.sheng_content = input("选择序号: 省--->打印市\nB/b(back/BACK)返回上级选项\
                \nQ/q(quit/QUIT)退出程序!\n输入你的选择:").strip()
                if self.sheng_content.isdigit():
                    self.sheng_content = int(self.sheng_content)
                    if 0 < self.sheng_content <= len(self.sheng_index):
                        self.sheng_content -= 1
                        self.city_shi = list(self.list[self.sheng_index[self.sheng_content]].keys())
                        #print(self.city_shi)
                        for s in self.city_shi:
                            print("\t{}\t{}\t".format(self.city_shi.index(s)+1, s))
    
                        self.shi()
                        return True
                    else:
                        print("输入的省序号不存在!")
                elif self.sheng_content.upper() == 'B' or self.sheng_content.upper() == 'BACK':
                    self.run()
                    return True
                elif self.sheng_content.upper() == 'Q'or self.sheng_content.upper() == 'QUIT':
                    exit()
                else:
                    print("error(非法字符)")
    
        def shi(self):
            while True:
                self.shi_content = input("选择序号 市---->打印县 :\nB/b(back/BACK)返回上级选项\
                \nQ/q(quit/QUIT)退出程序!\n输入你的选择:").strip()
                if  self.shi_content.isdigit():
                    self.shi_content = int(self.shi_content)
                    if 0 < self.shi_content <= len(self.city_shi):
                        self.shi_content = int(self.shi_content) - 1
                        self.shi_xian = \
                        list(self.list[self.sheng_index[self.sheng_content]][self.city_shi[self.sheng_content]])
                        sum1 = 0
                        for i in self.shi_xian:
                            sum1 +=1 < len(self.shi_xian)
                            print("\t{}\t{}\t".format(sum1, i))
                        self.xian()
                        return True
                    else:
                        print("输入的市序号不存在!")
                elif self.shi_content.upper() == 'B' or self.shi_content.upper() == 'BACK':
                    self.sheng()
                    return True
                elif self.shi_content.upper() == 'Q' or self.shi_content.upper() == 'QUIT':
                    exit()
                else:
                    print("输入错误!")
        def xian(self):
            while True:
                self.xian_content = input("***************end***************\nB/b(back/BACK)返回上级选项\
                \nT/t(top/TOP)返回顶部\nQ/q(quit/QUIT)退出程序!\n输入你的选择:").strip()
                if self.xian_content.upper() == 'B' or self.xian_content.upper() == 'BACK':
                    self.shi()
                    return True
                elif self.xian_content.upper() == 'T' or self.xian_content.upper() == 'TOP':
                    self.run()
                    return True
                elif self.xian_content.upper() == 'Q' or self.xian_content.upper() == 'QUIT':
                    exit()
                else:
                    print("输入错误")
    
    if __name__ == '__main__':
        st = city_info()

    运行效果展示

    download.gif

关键字