The following article describes how Python writes a three-level while loop menu
1. Define the dictionary, the Dictionary nested dictionary, the value of the embedded dictionary is a list.
Ideas:
City of Hubei Province: in the dictionary
Defines 3 dictionaries for storing
{sequence-key: city Name}
Shiqu_dir = {}
{sequence-key: County}
Xian_dir ={}
{sequence-key: Street}
Jiedao_dir = {}
The function corresponds to the sequence and key one by one.
For Number,key in Enumerate (Hbei_map.keys ()) gets the sequence key and city name stored in the city name Dictionary
Use a list of chose = [] to store the selected city-county-street
CHOSE[0],CHOSE[1],CHOSE[2] finally using slices to return
The intermediate input is all used in the judging mode for loop: for K,v in Xxx.items ():
if input = = k:
Lets the user enter only the numbers in the sequence. To use a judgment statement:
if int (input) not in dictionary. Keys ():
print (" you entered an illegal sequence, please enter the sequence {} ". Format (shiqu_list))
#-*-Coding:utf-8-*-__author__ = ' Hujianli ' import syshbei_map = {"Wuhan": {"Jiang ' an": ["Shanghai Street", "Tai Chi Street", "Yi Yuan Jie", "Station Street", "Street", "Yongqing Street", " Stadium Street, "West Street", "Taipei St.", "Labor Street," Huaqiao Street "," Hou Hu Jie "," Chenjiaji Street "," Qiaokou District ": [" Yi Jia Dun Jie dao "," Han Jia Dun Jie dao "," Zong Guan Jie dao "," Han River Bridge Street "," Baofeng Street "," Ronghua Street "," Chongren Street "," Hanzhong Street "], "Wuchang District": ["Jiyuqiao Street", "Tieji Road", "Xu Jia Peng Jie", "Xinhe Street"]}, "Jinzhou": {"Shashi District": ["Huangshigang", "Chongwen Street", "Jiefang Road Street"], "Jinzhou District": ["City South Street", "Xicheng Street", "Dongcheng Street", "Mashan Town", " Chuan Dian Zhen "]," Songzi ": [" New Jiangkou Town "," Nanhai Town "," Beibao Town "," Chen Dian Zhen "]," Gong ' an ": [" Hudi "," Huan "," Yang Jia Han Zhen "," Zhu Dang Zhen "]," Jianli County ": [" Rongcheng Town "," White Snail Town "," Zhu "]}," Xiaogan ": {" Xiaonan District ": [" Xiao Gang Zhen "," College Street "," Station Street "," Xinhua Street "]," City ": [" Town Street "," North Street "," Yang River Town "," Sanhe Town "]," Anlu ": [" Li Dian Zhen "," Patrol Town "," Gung Town "," Chen Dian Xiang "],},}# print ( hbei_map["Xiaogan" ["Should City"]) Shiqu_dir = {}xian_dir ={}jiedao_dir = {}def jiance (name): If len (name) = = 0:sys.exit ("\033[ 31;1m Select cannot be empty, clear re-enter ~~\033[0m ") elif Shiqu = =" Quit "or shiqu = =" Exit ": Sys.exit (" \033[31;1m exit program, Level three menu exit ~ ~ \033[0m ") el Se:return Trueprint ("========================================================================================== ==================== ") Print (" The city below Hubei province is as follows: ") for Number,keY in Enumerate (Hbei_map.keys ()):p rint (str (number+1) + ":" + str (key)) shiqu_dir[number+1] = Keychose = []shiqu_list = [X+1 for x in range (Len (Shiqu_dir.keys ()))]while True:print ("====================================================== ==================================================== ") Shiqu = input (" Please enter the serial number corresponding to the city you want to query, enter exit or quit automatically.: ") Jiance (shiqu) if int (shiqu) not in Shiqu_dir.keys ():p rint ("you entered an illegal sequence, please enter the sequence {}". Format (shiqu_list)) else:for keys, Vlaue in Shiqu_dir.items (): If int (shiqu) = = Keys:chose = [] Chose.append (Vlaue) print ("===== =========================================================================================== ") Print (" The county-town or area below the city is listed as follows: ") for Number,keys_xian in Enumerate (Hbei_map[vlaue].keys ()):p rint (str (number+1) +": "+ str ( Keys_xian)) xian_dir[number+1] = Keys_xianwhile true:print ("=========================================== ================================================== ") Xian= Input ("Please enter the serial number corresponding to the county you want to query, Quit/exit exit:") jiance (xian) xian_list = [x+1 for x in ran GE (Len (xian_dir.keys ()))]if Int (xian) not in Xian_dir.keys (): Print ("You entered an illegal sequence, please enter the sequence {}". Format ( xian_list)) Else:for k,v in Xian_dir.items (): If int (xian) = = K:chose.append (v) Jiedao_li = Hbei_map[chose[0]][chose[1]]for k,v in enumerate (jiedao_li):p rint (str (k+1) + ":" + str (v)) JIEDAO_DIR[K+1] = Vwhile true:print ("================================================================= ==================== ") Jiedao = input (" Please enter the serial number for the street to exit Please enter Quit/exit: ") Jiance (Jiedao) jiedao_list = [x+1 for x ' Range (len (Jiedao_dir.keys ()))]if Int (Jiedao) not In Jiedao_dir.keys ():p rint ("you entered an illegal sequence, please enter the sequence {}". Format (jiedao_list)) Else:for k,v in Jiedao_dir.items (): If Int ( Jiedao) = = k: Chose.append (v) print ("================================================================================= Print ("\033[35;1m your chosen city:%s County is:%s Street is:%s\033[0m"% (chose[0],chose[1],chose[2)) print ("========================== ========================================================= ") Breakbreakbreak