python三之三级菜单,python三三级菜单的访问

作者:计算机知识
#/usr/bin/env python
#yehui
'''
作业三:多级菜单

    三级菜单
    可依次选择进入各子菜单
    所需新知识点:列表、字典
'''
import readline
class MultiLevelMenu:
    def __init__(self):
        self.counts = 0
        self.multi_level_menu = {
            "这是第三级菜单!hello": {
                "这是第二级菜单!hi": "这是第一级菜单"
            }
        }
        for self.k3,self.v in self.multi_level_menu.items():
            self.menu3 = self.k3
            for self.k2,self.v1 in self.v.items():
                self.menu2 = self.k2
                self.menu1 = self.v1

    def choice_menu(self):
        print("1.%5s" % self.menu1)
        print("2.%5s" % self.menu2)
        print("3.%5s" % self.menu3)

        while self.counts <= 10:
            self.counts  = 1
            self.choic = input("请输入你想进入的菜单序列号,输入q退出: ")
            if self.choic == "1":
                print("欢迎进入一级菜单: %s" % self.menu1)
            elif self.choic == "2":
                print("欢迎进入二级菜单: %s" % self.menu2)
            elif self.choic == "3":
                print("欢迎进入三级菜单: %s" % self.menu3)
            elif self.choic == "q":
                exit("bye!")

if __name__ == '__main__':
    m = MultiLevelMenu()
    m.choice_menu()

#python三之三级菜单,python三三级菜单的访问。!/usr/bin/env python

#-*- coding:utf-8 -*-
#Author:gxli


#一级菜单项
def menu():
    #遍历字典dic一级菜单
    print('-----------一级菜单----------------')
    for index,key in enumerate(dic.keys(),1):
        print(index,key)
        ##将一级key的下标与key放入临时字典中
        dic_key[str(index)]=key
    choose = input("请选择1级菜单!退出输入 q ,返回请输入 b :")
    #判断输入,输入q时退出,输入b时提示无法返回
    if choose == 'q':
        quit()
    elif choose == 'b':
        print('目前为一级目录,无法返回!请重新选择!')
        return
    #判断用户输入是否在临时字典的key中,若有调用二级菜单
    elif dic_key.get(choose,0):
        menu_1(dic_key[choose])
    else:
        print('')
        print('您的输入有误,请重新输入!')


#二级菜单项
def menu_1(choose1):
    #输入有误时重新加载二级菜单项
    while True:
        print('')
        print('-----------二级菜单----------------')
        # 遍历字典dic二级菜单
        for index,key in enumerate(dic[choose1].keys(),1):
            print(index,key)
            ##将二级key的下标与key放入临时字典中
            dic_key[str(index)] = key
        choose2 = input("请选择2级菜单!退出输入 q ,返回请输入 b :")
        # 判断输入,输入q时退出,输入b时调用1级菜单
        if choose2 == 'q':
            quit()
        elif choose2 == 'b':
            menu()
        elif dic_key.get(choose2, 0):
            menu_2(choose1, dic_key[choose2])
        else:
            print('')
            print('您的输入有误,请重新输入!')

#三级菜单项
def menu_2(choose1, choose2):
    #输入有误时重新加载三级菜单项
    while True:
        # 遍历字典dic三级菜单
        print('')
        print('-----------三级菜单----------------')
        for index,key in enumerate(dic[choose1][choose2],1):
            #显示key与key的下标
            print(index,key)
            #将三级key的下标与key放入临时字典中
            dic_key[str(index)] = key
        choose3 = input("请选择3级菜单!退出输入 q ,返回请输入 b :")
        # 判断输入,输入q时退出,输入b时调用1级菜单
        if choose3 == 'q':
            quit()
        elif choose3 == 'b':
            menu_1(choose1)
        else:
            print('')
            print('您的输入有误,请重新输入!')


if __name__ == '__main__':
    # 设置菜单
    dic = {
        "动物": {
            "狗": ["大狗", "小狗"],
            "猫": ["大猫", "小猫"]
        },
        "植物": {
            "树": ["大树", "小树"],
            "花": ["大花", "小花"]
        }
    }
    dic_key = {}

    while True:
        menu()
 1 city = {
 2     "江苏省": {
 3         "南京市": {
 4             "栖霞区": ["aa", "bb"],
 5             "顺义区": ["cc", "dd"],
 6             "普陀区": ["ee", "ff"]
 7         },
 8         "南通市": {
 9             "通州区": ["gg", "hh"],
10             "港闸区": ["ii", "jj"],
11             "崇川区": ["kk", "ll"]
12         },
13     },
14     "浙江省": {
15         "杭州市": {
16             "A区": ["mm", "nn"],
17             "B区": ["oo", "pp"]
18         },
19         "温州市": {
20             "C区": ["qq", "rr"],
21             "D区": ["ss", "tt"]
22         }
23     }
24 }
25 current_layer = city
26 layers = []
27 while True:
28     print("欢迎来到省市区信息管理系统".center(50, "*"))
29     for layer in current_layer:
30         print(layer)
31     choice = input("请输入你要查看的省市区信息,返回上一级按b,退出按q").strip()
32     # 查看
33     if choice in current_layer:
34         layers.append(current_layer)
35         current_layer = current_layer[choice]
36     elif choice == "b":
37         current_layer = layers.pop()
38     elif choice == "q":
39         break
40     else:
41         print("到底了!")

# -*- coding:utf-8 -*-

 

# Author:Huajia

# meum 菜单,定义二个名叫meum的菜单

meum = {

    "北京":{

        "朝阳":{

            "国贸":["建外soho","嘻嘻"],

            "奥林匹克公园":["鸟巢","水立方"],

            "10捌里店":["王村路","东方盛泽之家"]

        },

        "海淀":{

            "上地":["上地三街","上地5街"],

            "菊园":["奔驰","宝马"]

        }

    },

本文由bwin必赢发布,转载请注明来源

关键词: 随笔 Python