Article From:https://www.cnblogs.com/zivli/p/9064696.html

Homework topic:Three level menu

  • Job requirements:

menu = {
    'Beijing':{
        'Haidian':{
            'Wudaokou':{
                'soho':{},
                'NetEase':{},
                'google':{}
            },
            'Zhongguancun':{
                'Iqiyi':{},
                'Car home':{},
                'youku':{},
            },
            'Land':{
                'Baidu':{},
            },
        },
        'Changping':{
            'Shahe':{
                'Uniqlo':{},
                'North Airlines':{},
            },
            'Tian Tong Yuan':{},
            'Huilongguan':{},
        },
        'Sunrise':{},
        'East City':{},
    },
    'Shanghai':{
        'Minhang':{
            "People's Square":{
                'Fried chicken shop':{}
            }
        },
        'a section of Shanghai':{
            'Train station':{
                'Ctrip':{}
            }
        },
        'Pudong':{},
    },
    'Shandong':{},
}
Demand:You can choose to enter each submenu in turnFrom any layer back to the upper layerExit the program from any layerA new point of knowledge: a list, a dictionary

Basic Edition:
while True:
    for i in menu:
        print(i)
    province = input("Please select the province: (exit by Q)")
    if province == 'Q':
        exit()
    else:
        print('Input error')
    if province in menu:
        while True:
            for i in menu[province]:
                print(i)
            city = input("Please select the city: (exit by Q, return by Q)")
            if city == 'Q':
                exit()
            if city == 'q':
                break
            else:
                print('Input error')
            if city in menu[province]:
                while True:
                    for i in menu[province][city]:
                        print(i)
                    county = input("Please select area or county: (exit by Q, Q return)")
                    if county == 'Q':
                        exit()
                    if county == 'q':
                        break
                    else:
                        print('Input error')
                    if county in menu[province][city]:
                        while True:
                            for i in menu[province][city][county]:
                                print(i)
                            choice = input('QExit, Q returns:')
                            if choice == 'Q':
                                exit()
                            if choice == 'q':
                                break
                            else:
                                print('Input error')

Loading press:

rank = menu
last_rank = []
while True:
    for i in rank:
        print(i)
    choice = input('>:').strip()
    if choice in rank:
        last_rank.append(rank)      # Add the current menu to the list
        rank = rank[choice]         # Go to the next menu
    elif choice == 'b':
        if len(last_rank) != 0:
            rank = last_rank.pop()  # Deletes the last element of the list and returns to the previous level.
    elif choice == 'Q':
        exit()

The inner layer and the outermost layer can also increase the corresponding hints.

Leave a Reply

Your email address will not be published. Required fields are marked *