menu = {' Beijing ': {' Haidian ': {' Five crossing ': {' Soho ': {}, ' NetEase ': {}, ' Goog Le ': {}}, ' Zhongguancun ': {' Iqiyi ': {}, ' Autohome ': {}, ' Youku ': {}, }, ' On the ground ': {' Baidu ': {},},}, ' Changping ': {' Shahe ': { ' Uniqlo ': {}, ' Beihang ': {},}, ' Tian Tong Yuan ': {}, ' Huilongguan ': {},}, ' Chaoyang ': {}, ' Dongcheng ': {},}, ' Shanghai ': {' Minhang ': {' People's Square ': {' Fried chicken Shop ': {}}}, ' Zhabei ': { ' Railway station ': {' Ctrip ': {}}}, ' Pudong ': {},}, ' Shandong ': {},} requirements: You can choose to go to each submenu to return from any level To the previous layer can exit the program from any level required new knowledge points: List, dictionary base version: While True:for I in Menu:print (i) Province = input ("Please select Province: (Press Q exit)") if PR Ovince = = ' Q ': Exit () else:print (' input error ') If province in Menu:while true:for I in Menu[province]: Print (i) City = input ("Please select the municipality: (Press Q to exit, Q to return)") If town = = ' 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 District or county: (press Q to exit, Q to return)") if County = = ' Q ': Exit () if county = = ' Q ': Break else:print (' input error ') If county in menu[province][city]: and true:for i in Menu[province][city][county]: print (i) choice = input (' q exit, Q return: ' If choice = = ' Q ': Exit () If choice = = ' Q ': Break else:print (' input error ') Loading version: Rank = Menulast_ran K = []while true:for i in Rank:print (i) Choice = input (';: '). Strip () If choice in Rank:last_r Ank.append (rank) # Add the current menu to the list rank = Rank[choice] # Go to the next level menu elif choice = = ' B ': If Len (last_ Rank)! = 0:rank = Last_rank.pop () # Deletes the last element of the list, thus returning to the previous layer elif choice = = ' Q ': Exit ()
Getting started with Python-level three menu