1. 程式人生 > >多級菜單的實現

多級菜單的實現

長沙 廣東 int true print nbsp elif bre 茄子

利用字典實現多級菜單:

area = {
    ‘廣西‘:{
        ‘梧州‘:{
            ‘岑溪‘:[‘豆腐釀‘,‘新圩米粉‘,‘茄子釀‘,‘白切雞‘],
            ‘容縣‘:[‘1‘,‘2‘,‘3‘],
            ‘蒼梧‘:[‘4‘,‘5‘,‘6‘]
            },
        ‘南寧‘:{
            ‘武鳴區‘:[‘大明山‘,‘伊嶺巖‘,‘靈水‘,‘明秀園‘],
            ‘賓陽縣‘:[‘魚生‘,‘賓陽酸辣粉‘]
            }
        },
    ‘廣東‘:{
        ‘廣州市‘:[‘越秀區‘,‘荔灣區‘,‘‘],
        ‘深圳市‘:[‘福田區‘,‘羅湖區‘,‘寶安區‘],
        ‘珠海市‘:[‘香洲區‘,‘鬥門區‘,‘金灣區‘]
        },
    ‘湖南‘:{
        ‘長沙市‘:[‘長沙縣‘,‘寧鄉縣‘,‘雨花區‘],
        ‘湘潭市‘:[‘雨湖區‘,‘湘潭縣‘,‘韶山市‘],
        ‘婁底市‘:[‘婁星區‘,‘新化縣‘,‘雙峰縣‘]
          }
}

while True:
    for i in area:
        print(i)

    choice = input(‘\n請輸入地方名:‘)
    if choice in area:
        while True:
            for i2 in area[choice]:
                print(‘\t‘,i2)
            choice2 = input(‘\n請輸入地方名:‘)
            if choice2 in area[choice]:
                while True:
                    for i3 in area[choice][choice2]:
                        print(‘\t\t‘,i3)
                    choice3 = input(‘\n請輸入地方名:‘)
                    if choice3 in area[choice][choice2]:
                            for i4 in area[choice][choice2][choice3]:
                                print(‘\t\t\t‘,i4)
                            choice4 = input(‘\n沒有啦,按回車返回上一級:‘)
                            if choice4 == ‘b‘:
                                pass
                            elif choice4 == ‘q‘:
                                exit()
                    if choice3 == ‘b‘:
                        break
                    elif choice3 == ‘q‘:
                        exit()
            if choice2 == ‘b‘:
                break
            elif choice2 == ‘q‘:
                exit()
    if choice == ‘q‘:
        exit()

  

多級菜單的實現