Python (5)-Simple exercise: Python Level three menu optimizer

Source: Internet
Author: User

Python Three-level menu optimizer, newbie link:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 menu ={    ‘北京‘:{        ‘海淀‘:{            ‘五道口‘:{                ‘soho‘:{},                ‘网易‘:{},                ‘google‘:{}            },            ‘中关村‘:{                ‘爱奇艺‘:{},                ‘汽车之家‘:{},                ‘youku‘:{},            },            ‘上地‘:{                ‘百度‘:{},            },        },        ‘昌平‘:{            ‘沙河‘:{                ‘北航‘:{},            },            ‘天通苑‘:{},            ‘回龙观‘:{},        },        ‘朝阳‘:{},        ‘东城‘:{},    },    ‘上海‘:{        ‘闵行‘:{            "人民广场":{                ‘炸鸡店‘:{}            }        },        ‘闸北‘:{            ‘火车战‘:{                ‘携程‘:{}            }        },        ‘浦东‘:{},    },    ‘山东‘:{},}current_layer=menu        #当前层last_layers=[menu]        #上一层whileTrue:    for key incurrent_layer:         #打印第一层菜单        print(key)    choice=input(">>:").strip()       #选择第二层菜单    ifchoice incurrent_layer:                    last_layers.append(current_layer)        #进入下一层菜单前,把当前层菜单加入上一次菜单中        current_layer=current_layer[choice]      #当前层菜单被重新定义,进入循环打印下一层菜单    ifchoice==0:                                #选择菜单层为空,结束本次循环        continue    ifchoice=="q":                             #选择菜单层为“q”,结束本层循环        break    ifchoice=="b":                             #选择菜单层为“b”,返回上一层菜单        current_layer=last_layers[-1]           #返回上一层菜单前,当前层被重新定义        last_layers.pop()                       #删除最后一次进入下一层菜单所加入的上一层列表数据print("程序结束!")


Python (5)-Simple exercise: Python Level three menu optimizer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.