Requirements:
1, Level three menu
2, in turn, choose to enter the submenu, select serial number into the directory
3, input B return to the superior directory, Q exit change directory
Code implementation:
#!
/bin/env python #!--*--coding:utf-8--*--#----*auth:freem* Import sys #flag =true #def flas (): #flag =false; #定义字典包括, country, State (province), City dic={' China ': {' Hebei ':(' Shijiazhuang ', ' Xingtai ', ' Hengshui ', ', ' Beijing ':(' changping ', ' Haidian ', ' Chaoyang ', ', ' Shanxi ':(' Taiyuan ', ' Datong ', ' Yun Cheng ',)}, ' United States ': {' New York State ' :(' New York ', ' Rochester ', ' Buffalo ',), ' California ':(' Los Angeles ', ' Hollywood ', ' Alhambra ', ', ' Pennsylvania ':(' Philadelphia ', ' Bethlehem ', ' Washington ',)}, ' Australia ': {' Victoria ':(' Melbourne ', ' Geelong ', ' Barry Dee ',, ' State Of Western Australia ':(' Mazar ', ' Perth ', ' Freeman ',], ' South Australia ':(' Adelaide ', ' inland ', ' Rosa '),}} #print (dic[' China ' [' Hebei ']) Dic_dir={1: "China ", 2:" The USA ", 3:" Australia ",} #定义国家跟序号的对应关系 Dic_dirtt={1: (" Hebei "," Beijing "," Shanxi "), 2: (" New York State "," California "," Pennsylvania ",), 3: (" Victoria "," State Of Western Australia ") , "South Australia",)} #省 (state) Correspondence #dic_dir01 ={1: "China", 2: "The USA", 3: "Australia",} #dic_dir02 ={1: "China", 2: "The USA", 3: "Australia", "' For I" (1, DIC.__LEN__ () +1): Print (I, ":", Dic.pop ()) ' #print (dic["China"] ["Hebei"][1]) #print (["Hebei"][1]) def loop (): While True:for I In range (1,dic.__len__ () +1): #遍历字典长度, generate ordinal print (i, ': ', Dic_dir[i]) dir01=input ("Please input num or q[quit]:",) # Select the corresponding ordinal input and enter the return to the parent directory ID if Dir01 in [' 1 ', ' 2 ', ' 3 ']: #确认输入序号是否在 [' 1 ', ' 2 ', ' 3 '], for the J in range (1,dic.__len__ () +1): If int (Dir01) ==j:print (">>", J, ': ', dic_dir[j], "\ n") #打印所序号所对应的国家 for-I in range (1, Dic[dic_dir[j]].__len __ () + 1): Print (">>>>", I, ': ', dic_dirtt[j][i-1]) #遍历输出所选序号国家对应的省/state Dir02 = input ("Please input num or b[back] or Q[quit]: ",) #输入上级所需国家对应的省的序号 if dir02 in [' 1 ', ' 2 ', ' 3 ']: with K in range (1, dic[dic_dir[j]].__len__ () + 1): #遍历省所对应字典所对 Expected length, generate serial number if int (dir02) = = K:print ("*******", K, ":", dic_dirtt[j][k-1], "\ n") for h in range (1, dic[dic_dir[j]][dic_dirtt[ J][K-1]].__LEN__ () + 1): Print ("******* ^_^ >>>>", H, ': ', dic[dic_dir[j]][dic_dirtt[j][k-1]][h-1]) # Print the city print of the final selected province ("------------------------------------------------") # print (k, ': ', dic_dirtt[j][k]) elif dir02= = ' B ': continue;
#跳出上一级循环 elif dir02== ' q ': return #跳出整个循环 #return False; Else:print ("Please insert correct str,strat with higher dir!") #print (i, ': ', Dic[dic_dir[j]]) # print (Dic[dic_dir[j]) E Lif dir01== ' Q ': break; #退出 else:print ("Please insert correct str!") if __name__== "__main__": Loop ()
Implementation effect:
1 Normal input, enter serial number 1, display "China" sub Directory, then enter 1, show "Hebei" sub-directory, as shown in the following figure:
2, direct input [Q], exit the program, as shown in the following figure:
3, the first level of the normal input directory, the second level of the directory input [b], return to the superior directory, as shown in the following figure:
4, the first level of the normal input directory, the second level of directory input [Q], directly exit the entire program, as shown in the following figure:
Note: This procedure is only applicable to python3.0 or above, if python2.7, please amend yourself;
The above is a small series to introduce the Python level three directory display to achieve the method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!