Requirements: You can choose to go to each submenu from any level back to the previous layer you can exit the program from any level required new knowledge points: List, dictionary
Use only one while loop
1 #!-*-coding:utf-8-*-2 3menu = {4 'Beijing': {5 'Haidian': {6 'Five crossing': {7 'Soho': {},8 'NetEase': {},9 'Google': {}Ten }, One 'Zhongguancun': { A 'Iqiyi Art': {}, - 'Autohome': {}, - 'Youku': {}, the }, - 'on the ground': { - 'Baidu': {}, - }, + }, - 'changping': { + 'Shahe': { A 'old boy': {}, at 'Beihang University': {}, - }, - 'Tin Tong Court': {}, - 'Huilongguan': {}, - }, - 'Chaoyang': {}, in 'Dongcheng': {}, - }, to 'Shanghai': { + 'Minhang': { - "People's square": { the 'Fried Chicken Shop': {} * } $ },Panax Notoginseng 'Zhabei': { - 'Train Warfare': { the 'Ctrip': {} + } A }, the 'Pudong': {}, + }, - 'Shandong': {}, $ } $Current_layer = Menu#Implementing Dynamic Loops -Parent_layer = []#Keep all parent layers, and the last element is always the parent - the whileTrue: - Print('-'* 10,"Menu",'-'* 10)Wuyi forIinchCurrent_layer:#traverse the print address the Print(i) - Print("Please enter the menu name below, or B: Return to the previous layer, Q: Exit \ n",'-'* 26) WuChoice = input (">>>"). Strip () - ifChoiceinchCurrent_layer: About ifCurrent_layer[choice]:#determine if the final layer $Parent_layer.append (Current_layer)#Add the current layer as the parent layer before entering the child layer -Current_layer = Current_layer[choice]#Modify a child layer - Else: - Print('current is the last page') A elifChoice = ="': + Continue the elifChoice = ='b' orChoice = ='B':#back to top - ifParent_layer:#determine if the Parent_layer is empty $Current_layer = Parent_layer.pop ()#Remove the current layer parent layer the #Exit Loop the elifChoice = ='Q' orChoice = ='Q': the Break the Else: - Print("\033[34;1m input Error, please re-enter \033[0m")
Python implementation level Three menu