Python_ achieve Level Three catalog display

Source: Internet
Author: User

Requirements:

1, Level three menu

2, you can choose to enter the sub-menu, select the serial number into the directory

3, enter B to return to the parent directory, q exit change directory

Code implementation:

1 #! /bin/env python2 #!--*--coding:utf-8--*--3 #----*auth:freem*4 ImportSYS5 #flag=true6 #def flas ():7     #Flag=false;8 #definition dictionary includes, State, state (province), City9dic={'China':{'Hebei':('Shijiazhuang','Xingtai','Hengshui',),'Beijing':('changping','Haidian','Chaoyang',),'Shanxi':('Taiyuan','Datong','Shipping Process',)},'United States':{'New York State':('New York',' Rochester','Buffalo',),'California':('Los Angeles','Hollywood','Alhambra',),'Pennsylvania State':('Philadelphia','Bethlehem','Washington',)},'Australia':{'Victoria':('Melbourne','Geelong','Barry Dee',),'State Of Western Australia':('Mazar','Perth','Freeman Chart',),'South Australia':('Adelaide','Inland','Barossa'),}}Ten #print (dic[' China ' [' Hebei ']) OneDic_dir={1:"China", 2:"United States", 3:"Australia",}#define the correspondence between the country and the serial number ADic_dirtt={1: ("Hebei","Beijing","Shanxi"), 2: ("New York State","California","Pennsylvania State",), 3: ("Victoria","State Of Western Australia","South Australia",)}#Province (state) correspondence relationship - #dic_dir01={1: "China", 2: "United States", 3: "Australia",} - #dic_dir02={1: "China", 2: "United States", 3: "Australia",} the " "For i in range (1,dic.__len__ () +1): - Print (I, ":", Dic.pop ())" " - #print (dic["China" ["Hebei"][1]) - #print (["Hebei"][1]) + defLoop (): -      whileTrue: +          forIinchRange (1,dic.__len__() +1):#Traverse dictionary length, generate sequence number A             PrintI':', Dic_dir[i]) atDir01=input ("Please input num or q[quit]:",)#Select the corresponding ordinal input and input to return to the parent directory ID -         ifDir01inch['1','2','3']:#confirm that the input sequence number is in [' 1 ', ' 2 ', ' 3 '] -              forJinchRange (1,dic.__len__() +1): -                 ifint (DIR01) = =J: -                     Print(">>"J':', Dic_dir[j],"\ n")#print the country corresponding to the serial number -                      forIinchRange (1, dic[dic_dir[j]].__len__() + 1): in                         Print(">>>>"I':', Dic_dirtt[j][i-1])#Traverse output selected serial number the state corresponding to the province/State -Dir02 = input ("Please input num or b[back] or Q[quit]:", )#Enter the serial number of the province corresponding to the desired country to                     ifDir02inch['1','2','3']: +                          forKinchRange (1, dic[dic_dir[j]].__len__() + 1):#traverse the length of the dictionary corresponding to the province, generating the sequence number -                             ifint (dir02) = =K: the                                 Print("*******"K":", Dic_dirtt[j][k-1],"\ n") *                                  forHinchRange (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 corresponding to the province of the final selectionPanax Notoginseng                                 Print("------------------------------------------------") -                                #print (k, ': ', dic_dirtt[j][k]) the                     elifdir02=='b': +                         Continue;#jump out of the top loop A                     elifdir02=='Q': the                         return #jump out of the loop +                             #return False; -                     Else: $                         Print("Please insert correct str,strat with higher dir!") $                  #print (i, ': ', Dic[dic_dir[j]]) -                    #print (Dic[dic_dir[j]]) -         elifdir01=='Q': the               Break;#Exit -         Else:Wuyi             Print("Please insert correct str!") the  - if __name__=="__main__": WuLoop ()

Implementation results:

1) Normal input, enter the serial number 1 respectively, show "China" sub-directory, and then enter 1, showing "Hebei" sub-directory, as shown:

2), direct input [Q], exit the program as shown:

3), the first level directory normal input, the second level directory input [b], return to the parent directory, as shown in:

4), the first level directory normal input, the second level directory input [Q], directly exit the entire program, as shown in:

Note: This procedure is only applicable to python3.0 above, if python2.7, please modify it yourself;

If interested, please try it yourself;

Python_ achieve Level Three catalog display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.