#!/usr/bin/env python#-*-coding:utf-8-*-#------------------------------------------------------#Create a level three menu that requires:#you can choose to go to the appropriate menu#can be executed at any level to return the last or exit#-------------------------------------------------------Print("Three-level menu display of provincial and municipal districts") DiC= { 'Shaanxi Province':{ 'Xi ' an city':['Lianhu District','Changan District','Chanba Ba District','Beilin District'], 'Weinan':['Linwei District','Hua Zhou District','Dali County','Pucheng County'], 'Hanzhong':['Hantai District','Nanzheng District','Chenggu County','Xixiang County'], }, 'Zhejiang province':{ 'Hangzhou City':['West L. District','Jianggan District','Shangcheng District','Xiacheng District'], 'Wenzhou':['Lucheng District','Longwan District','Ouhai','Dongtou District'], 'Taizhou':['Jiaojiang','Huangyan District','Huangyan District','Linhai'], }, 'Henan Province':{ 'Zhengzhou':['Zhongyuan District','Erqi District','Tube City','Jinshui District'], 'Kaifeng':['gu Lou','Longting District','Shun Ho District','Xiang Character Area'], 'Luoyang':['Jianxi District','Xigong District','Old Town','Luolong District'],},}sheng_list=list (Dic.keys ())#get list of provinces whileTrue:Print("Province". Center (50,'*'))#Print province title bar, length 50, center character forIinchsheng_list:Print(Sheng_list.index (i) +1,i)#for Loop reads the list of provinces and numbers the list readSheng_id=input ("Please enter the province number, exit Please enter Q:") ifSheng_id.isdigit ():#determine if the number entered is a numberSheng_id=int (sheng_id)#Rounding the number of inputs ifsheng_id >0 andsheng_id <=len (sheng_list):#determines whether the input number is greater than 0 and is less than or equal to the length of the province listSHENG_NAME=SHENG_LIST[SHENG_ID-1]#gets the name in the province list, which starts from 0 in the list, so it needs to be reduced by 1Shi_list=list (Dic[sheng_name].keys ())#Get City List whileTrue:Print("City". Center (50,"*"))#Print City title bar, length 50, center character forIinchshi_list:Print(Shi_list.index (i) +1,i)#for Loop reads a list of cities and numbers the list readShi_id=input ("Please enter the city number, return please enter B, exit Please enter Q:") ifShi_id.isdigit ():#determine if the number entered is a numberShi_id=int (shi_id)#Rounding the number of inputs ifshi_id >0 andShi_id<=len (shi_list):#determines whether the input number is greater than 0 and is less than or equal to the length of the province listSHI_NAME=SHI_LIST[SHI_ID-1]#get the name of the city list, the default is 0 starting from the list, so you need to subtract 1Xian_list=dic[sheng_name][shi_name]#get a list of counties whileTrue:Print("Counties". Center (50,"*"))#Print county title bar, length 50, center character forIinchxian_list:Print(Xian_list.index (i) +1,i)#for Loop reads the county list and numbers the list readBack_or_quit=input ("return to the previous level please enter B, exit Please enter Q:") ifback_or_quit=='b':#determines whether the character entered is equal to the character B Break #if it is, end the loop and return to the previous loop elifback_or_quit=='Q':#determines whether the character entered is equal to the character QExit ()#If yes, exit the entire program Else: Print("\033[31;1m Illegal input! \033[0m")#Enter non-digital content at the county level, prompting for illegal content Else: Print("\033[31;1m number%s does not exist!\033[0m"% (shi_id))#Otherwise, the user number does not exist in the bold red font elifshi_id=='b':#Enter B in the city directory to return to the provincial level directory Break elifshi_id=='Q': Exit ()#Q exits the program at the City level directory entry Else: Print("\033[31;1m Illegal input! \033[0m")#Enter non-digital content at the city level, prompting for illegal content Else: Print("\033[31;1m number%s does not exist!\033[0m"% (sheng_id))#enter data other than >0,<=3 in the provincial directory, you are prompted to enter a non-existent elifsheng_id=='Q':#at the provincial directory input, Q exits the program because the province is the first loop, so the break ends the loop as if it were exiting the program Break Else: Print("\033[31;1m Illegal input! \033[0m")
Python Learning basics-writing level three menus