python-多级菜单

主题: 用python编写一个多级菜单
要求:

  1. 三级菜单
  2. 可依次进入子菜单

知识点:

  1. 字典


此例中,"value"用到了"字典"和"列表"两种类型,如下所示:

点击查看代码
# 先初始化一个菜单
menu = {'1': {'1.1': ['1.1.1', '1.1.2', '1.1.3'],
              '1.2': ['1.2.1', '1.2.2', '1.2.3'],
              '1.3': ['1.3.1', '1.3.2', '1.3.3']},
        '2': {'2.1': ['2.1.1', '2.1.2', '2.1.3'],
              '2.2': ['2.2.1', '2.2.2', '2.2.3'],
              '2.3': ['2.3.1', '2.3.2', '2.3.3']},
        '3': {'3.1': ['3.1.1', '3.1.2', '3.1.3'],
              '3.2': ['3.2.1', '3.2.2', '3.2.3'],
              '3.3': ['3.3.1', '3.3.2', '3.3.3']}}

其中'1','2'和'3'是一级菜单,也是字典menu的'key',对应的'value'是{}下的字典类型,构成了二级菜单,二级菜单对应的'value'是[]下的列表类型,构成第三级菜单。

其他知识,诸如创建字典,访问字典中的内容等,详见 https://www.runoob.com/python3/python3-dictionary.html

  1. 列表

其他相关知识可参考 https://www.runoob.com/python3/python3-list.html

实现代码

点击查看代码
# -*- coding: utf-8 -*-
# This code if for a multi-menu
# @Software: Pycharm
# @Author: lm
# @Time: 2022-01-27

# 要求:1. 三级菜单;2. 可依次进入子菜单
# 知识点:列表、字典

# 先初始化一个菜单
menu = {'1': {'1.1': ['1.1.1', '1.1.2', '1.1.3'],
              '1.2': ['1.2.1', '1.2.2', '1.2.3'],
              '1.3': ['1.3.1', '1.3.2', '1.3.3']},
        '2': {'2.1': ['2.1.1', '2.1.2', '2.1.3'],
              '2.2': ['2.2.1', '2.2.2', '2.2.3'],
              '2.3': ['2.3.1', '2.3.2', '2.3.3']},
        '3': {'3.1': ['3.1.1', '3.1.2', '3.1.3'],
              '3.2': ['3.2.1', '3.2.2', '3.2.3'],
              '3.3': ['3.3.1', '3.3.2', '3.3.3']}}

# 为了方便交互,创建索引列表
first_index = [(index, key) for index, key in enumerate(menu)]
first_index.append((len(first_index), '退出'))     # 增加退出选项

while True:
        print('\n')
        print("这里是一级菜单:")
        print('---------------------------------')
        # 打印索引列表
        for i in first_index:
                for j in i:
                        print(j, end=' ')
                print('')

        # 开始交互,用户选择一级菜单
        get_first_index = input("请选择查询索引号:")

        # 根据输入情况分类做出反应
        if not get_first_index.isdigit():
                print("请输入数字索引号:")
                continue
        elif int(get_first_index) < 0 or int(get_first_index) > len(first_index):
                print("请输入正确的索引号:")
                continue
        elif int(get_first_index) == len(first_index) - 1:
                print("谢谢使用,再见!")
                break
        else:
                choose_index1 = first_index[int(get_first_index)][1]
                second_index = [(index, key) for index, key in enumerate(menu[choose_index1])]
                second_index.append((len(second_index), '退出')) # 增加退出选项
                while True:
                        print("这里是二级索引:")
                        print("--------------------------------------")
                        for i in second_index:
                                for j in i:
                                        print(j, end=' ')
                                print('')

                        # 开始交互,用户选择一级菜单
                        get_second_index = input("请选择想要查询的索引号:")

                        # 根据输入情况分类做出反应
                        if not get_second_index.isdigit():
                                print("请输入数字索引号:")
                                continue
                        elif int(get_second_index) < 0 or int(get_second_index) > len(second_index):
                                print("请输入正确的索引号:")
                                continue
                        elif int(get_second_index) == len(second_index) - 1:
                                print("已返回上一级菜单。。。")
                                break
                        else:
                                choose_index2 = second_index[int(get_second_index)][1]
                                print(menu[choose_index1][choose_index2])
                                print('---------------------------------')



2022-01-27男朋友安全回家了,开心,虽然不能见面,但是想到他终于可以回家过年就很开心,哪里都比不上家里的那种舒服。何以庆祝,唯有学习。

posted @ 2022-01-27 14:31  万国码aaa  阅读(249)  评论(0编辑  收藏  举报