Python three-level catalog presentation method

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:

#! /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 ', '}, ' USA ': {' New York State ':(' New York ', ' Rochester ', ' Buffalo ', ' California ':(' Los Angeles ', ' Hollywood ', ' Alhambra ', ' Pennsylvania ':(' Philadelphia ', ' Bethlehem ', ' Washington ', ')}, ' Australia ': {' Victoria ':(' Melbourne ', ' Geelong ', ' Barry ', ') , ' State Of Western Australia ':(' Mazar ', ' Perth ', ' Freeman ', ' South Australia ':(' Adelaide ', ' inland ', ' Rosa '),}} #print (dic[' China ' [' Hebei ']) Dic_dir={1: "China", 2: "United States", 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: "USA", 3: "Australia", "#dic_dir02 ={1:" China ", 2:" United States ", 3:" Australia "," "" For I in range (1,dic.__len__ () +1):p rint (i, ":", Dic.pop ()) "#print (dic[" China "[" Hebei "][1]) #print ([" Hebei "][1]) def loop (): While True:for I in range (1, DIC.__LEN__ () +1): #遍历字典长度, generate the serial number print (I, ': ', Dic_dir[i]) dir01=input ("Please input num or q[quit]:",) # Select the corresponding ordinal input and the input to return the parent directory ID if Dir01 in [' 1 ', ' 2 ', ' 3 ']: #确认输入序号是否在 [' 1 ', ' 2 ', ' 3 '] medium for 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):p rint (">> >> ", I, ': ', dic_dirtt[j][i-1]) #遍历输出所选序号国家对应的省/state Dir02 = input (" Please input num or b[back] or Q[quit]: ",) #输入上级所需国家对 The province should be ordinal if dir02 in [' 1 ', ' 2 ', ' 3 ']:for K in range (1, dic[dic_dir[j]].__len__ () + 1): #遍历省所对应字典所对应的长度, generate ordinal 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 ("------- -----------------------------------------") # 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]]) elif dir01== ' Q ': break; #退出else:p rint ("Please insert correct str!") If __name__== "__main__": Loop ()

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;

The above is a small series to introduce you to the Python three-level directory display implementation method, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for your support for topic.alibabacloud.com!

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.