• Python 基础


    作业要求

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

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

    3. 菜单数据保存在文件中

    4. 让用户选择是否要退出

    5. 有返回上一级菜单的功能

    评分标准:

    用多层嵌套while循环的方式完成作业2,85分

    只用一层循环完成作业2,100分

    知识点总结:

    1. 打开文件读取 字典信息, 用eval 从 字符串 转到 dictionary --- 没有成功

    2. while, for 循环的掌握。while true的放的位置

    3. 可以用function 的方式定义每次菜单打开,但是存在bug,最终用了比较low的方式完成作业。

    My Work: 

    #!usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author: Jane Yue
    # Key points: Dictionary;
    
    import sys
    
    menu_dict = {
        "华南":{
            "广东":["广州市","佛山市","深圳市","东莞市"],
            "广西":["南宁市","柳州市","桂林市","北海市"],
            "海南":["海口市","三亚市","三沙市","儋州市"]},
        "华东":{
            "上海":{"浦东新区": ['陆家嘴', '张江科创园', '世纪公园'],
                  "卢湾区": ['日月光商场','K11'],
                  "徐汇区": ['港汇', '环贸ipam'],
                  "长宁区": ['中山公园'],
                  "普陀区": ['真如']},
            "安徽":["合肥市","芜湖市","淮南市","马鞍山市"],
            "江苏":["南京市","无锡市","徐州市","常州市","苏州市"]},
        "华北":{
            "北京":["东城区","西城区","朝阳区","丰台区","石景山区","海淀区"],
            "山西":["太原市","大同市","阳泉市","长治市"],
            "河北":["石家庄市","唐山市","秦皇岛市","邢台市"]},
        "华中":{
            "湖北":["武汉市","黄石市","十堰市","十堰市"],
            "河南":["郑州市","开封市","洛阳市","平顶山市"],
            "湖南":["长沙市","株洲市","衡阳市","邵阳市"]},
        "西南":{
            "重庆":["万州区","涪陵区","渝中区","大渡口区"],
            "四川":["成都市","自贡市","攀枝花市","德阳市"],
            "贵州":["贵阳市","六盘水市","遵义市","安顺市"],}
    }
    
    while True:
        for i in sorted(menu_dict):   # 打印第一层
            print(i)
    
        choice_1 = input('1. 请选择您要查询的区域 【q:退出】 >>> ')
        if choice_1 in menu_dict:
            while True:
                for j in menu_dict[choice_1]:   #打印第二层
                    print('	',j)
                choice_2 = input('2. 请选择您要查询的区域 【q:退出; b:返回】 >>>')
                if choice_2 in menu_dict[choice_1]:
    
                    while True:
                        for k in menu_dict[choice_1][choice_2]:
                            print('		', k)
                        choice_3 = input('3. 请选择您要查询的区域。 请选【q:退出; b:返回】 >>>')
                        if choice_3 in menu_dict[choice_1][choice_2]:
                            for l in menu_dict[choice_1][choice_2][choice_3]:
                                print('			', l)
                            choice_4 = input('4. 最后一层。 请选择【b:返回】或任意键退出')
                            if choice_4 == 'b':
                                continue
                            else:
                                sys.exit()
    
                        if choice_3 == "b":
                            break
                        else:
                            print('感谢阅览')
                            sys.exit()
    
                elif choice_2 =="b":
                    break
                elif choice_2 == "q":
                    print('感谢阅览')
                    sys.exit()
                else:
                    print('请输入正确选项')
                    continue
        elif choice_1 == "q":
            print('感谢阅览')
            sys.exit()
        else:
            print('请输入正确选项')
            continue
    View Code

    SAMPLE 1 

    data = {
        '北京': {
            '海淀': {
                '五道口': {
                    'soho': {},
                    '网易': {},
                    'Google': {},
                },
                '中关村': {
                    '爱奇艺': {},
                    '汽车之家': {},
                    'youku': {},
                },
                '上地': {
                    'baidu': {},
                },
            },
            '昌平': {
                '沙河': {
                    'oldboy': {},
                    '北航': {},
                },
                '天通苑': {},
                '回龙观': {},
            },
            '朝阳': {},
            '东城': {},
        },
        '上海': {
            "黄浦": {
                '人民广场': {
                    '炸鸡店': {},
                },
            },
            '闸北': {
                '火车站': {
                    '携程': {},
                },
            },
            '浦东': {},
        },
        '山东': {},
    }
    
    exit_flag = False
    
    while not exit_flag:
        for i in data:
            print(i)   # 进入死循环
    
        choice = input("your option >>>:")
        if choice in data:
            while not exit_flag:
                for j in data[choice]:
                    print("	", j)
                choice2 = input('your 2nd option >>>:')
                while not exit_flag:
                    for k in data[choice][choice2]:
                        print('		', k)
                    choice3 = input('your 3rd option >>>:')
                    if choice3 in data[choice][choice2]:
                        for l in data[choice][choice2][choice3]:
                            print('		', l)
                        choice4 = input("final, please input b for exit")
                        if choice4 == 'b':
                            pass  # pass == 啥都不做,必须写否则报错。作为占位符
                        elif choice4 == "q":
                            exit_flag = True
                    if choice3 == 'b':
                        break
                    elif choice3 == "q":
                        exit_flag = True
                if choice2 == 'b':
                    break
                elif choice2 == "q":
                    exit_flag = True
    View Code
  • 相关阅读:
    Dynamic Shortest Path CodeForces
    New task CodeForces
    New Roads CodeForces
    Music in Car CodeForces
    互联网思维体系--史上最全的互联网思维精髓总结
    OpenGL Shader Key Points (3)
    Netfilter的使用和实现
    数据包接收系列 — IP协议处理流程(二)
    数据包接收系列 — IP协议处理流程(一)
    数据包接收系列 — NAPI的原理和实现
  • 原文地址:https://www.cnblogs.com/lg100lg100/p/7123717.html
Copyright © 2020-2023  润新知