python学习-字典1

  第一次写技术博客,也是刚刚申请成功,所以要先感谢博客园的相关工作人员。开发做了多年,直到今年才开始学习python,习惯了C、C++的数据格式,所以一开始就被python数据类型中的字典所吸引。基础的知识有很多同仁都写过了,我只是通过我学习中的一道作业题来说明一下。

  题目:三级菜单

  要求:

        1. 运行程序输出第一级菜单

        2. 选择一级菜单某项,输出二级菜单,同理输出三级菜单

        3. 返回上一级菜单和顶部菜单

        4. 菜单数据保存在文件中"

实现代码:

#准备一个三级菜单
复制代码
menu_list = {"甘肃":{"兰州":{"城关区":{1:"甘肃地质博物馆",
                                         2:"地球厅",
                                         3:"矿物岩石厅"},
                              "七里河区":{1:"兰州碑林",
                                           2:"碑轩",
                                           3:"碑亭"},
                              "西固区":{1:"五一山生态旅游区",
                                         2:"合欢",
                                         3:"水杉",
                                         4:"枇杷"},
                              "红古区":{1:"白塔山公园",
                                         2:"象皮鼓",
                                         3:"青铜钟",
                                         4:"紫荆树",}
                            },
                      "敦煌":{"沙州镇":{"敦煌莫高窟":"5A",
                                         "鸣沙山-月牙泉风景名胜区":"4A",
                                         "阳关遗址":"4A",},
                              "肃州镇":{"甘肃敦煌雅丹国家地质公园":"4A",
                                         "白马塔":"1A"},
                            },
                   },
             "宁夏":{"中卫市":{"中宁":{"美食":"臊子面",
                                         "景点":"双龙寺",
                                         "特产":"枸杞",},
                                "中卫":{"美食":"馓子",
                                         "景点":"沙坡头",
                                         "特产":"羊皮筏子",},
                          },
                      "吴忠":{"利通区":{1:"柴园村",
                                         2:"李园村",
                                         3:"石佛寺村"},
                               "青铜峡市":{1:"刘碱滩村",
                                            2:"洼路沟村",
                                            3:"二道桥村"},
                               "盐池县":{1:"杨岔村",
                                          2:"吴家桥村",
                                          3:"郭家桥村",
                                          4:"刘家湾村",},
                               "同心县": {1:"山水沟村",
                                          2:"清水沟村",
                                          3:"涝河桥村",
                                          4:"马家湾村",},
                        },
                      "银川":{"银川市":{1:"海宝塔",
                                         2:"华夏西部影视城",
                                         3:"纳家户清真寺",
                                         4:"中大寺和南关清真大寺",},
                              "西夏区":{"景区":"西夏王陵"},
                            },
                },
        }
#一个死循环,为的是能反复输入,直到输入q退出
while True:
    index = 0
    for i in menu_list.keys():
        print(str(index) + ":", i)
        index += 1

    oper = str(input("province>:"))

    if oper == "q":   #判断是否退出
        exit()
    elif oper not in menu_list.keys():  #判断输入是否合规
        print("输入内容不存在,请重新输入!")
    else:  
        one = menu_list[oper]
        print("***************************************")

        while True:   #进入二级菜单
            index = 0
            for i in one.keys():
                print(str(index) + ":", i)
                index += 1

            oper = str(input("city>:"))

            if oper == "q": #是否退出判断
                exit()
            elif oper == "b": #是否返回一级菜单
                break
            elif oper not in one.keys():
                print("输入内容不存在,请重新输入!")
            else:
                two = one[oper]
                print("***************************************")

                while True: #进入三级菜单
                    index = 0
                    for i in two.keys():
                        print(str(index) + ":", i)
                        index += 1

                    oper = str(input("area>:"))

                    if oper == "q":
                        exit()
                    elif oper == "b":
                        break
                    elif oper not in two.keys():
                        print("输入内容不存在,请重新输入!")
                    else:
                        print(two[oper])
                        print("***************************************")

转载于:https://www.cnblogs.com/maoxiong1985/p/7830774.html

你可能感兴趣的:(python,c/c++)