1menu = {2 'Beijing':{3 'Haidian':{4 'Five crossing':{5 'Soho':{},6 'NetEase':{},7 'Google':{}8 },9 'Zhongguancun':{Ten 'Iqiyi Art':{}, One 'Autohome':{}, A 'Youku':{}, - }, - 'on the ground':{ the 'Baidu':{}, - }, - }, - 'changping':{ + 'Shahe':{ - 'Beihang University':{}, + }, A 'Tin Tong Court':{}, at 'Huilongguan':{}, - }, - 'Chaoyang':{}, - 'Dongcheng':{}, - }, - 'Shanghai':{ in 'Minhang':{ - "People's square":{ to 'Fried Chicken Shop':{} + } - }, the 'Zhabei':{ * 'Train Warfare':{ $ 'Ctrip':{}Panax Notoginseng } - }, the 'Pudong':{}, + }, A 'Shandong':{}, the}
The code is as follows:
1Current_layer=menu#Current Layer2Last_layers=[menu]#up a layer3 whileTrue:4 forKeyinchCurrent_layer:#Print the first level menu5 Print(Key)6Choice=input (">>:"). Strip ()#Select the second level menu7 ifChoiceinchCurrent_layer:8Last_layers.append (Current_layer)#Add the current layer menu to the previous menu before entering the next level menu9Current_layer=current_layer[choice]#The current layer menu is redefined and enters the loop to print the next level menuTen ifChoice==0:#Select menu layer is empty, end this loop One Continue A ifchoice=="Q":#Select the menu layer as "Q" to end this layer loop - Break - ifchoice=="b":#Select the menu layer as "B" to return to the previous level menu theCURRENT_LAYER=LAST_LAYERS[-1]#The current layer is redefined before returning to the previous layer menu -Last_layers.pop ()#deletes the last layer of list data added to the next level of menu entry - Print("The program is over! ") -
Simple exercise: Python Level three menu optimizer