python editing three-level directory

1, Demand analysis

The three-level directory shall be able to realize the following requirements:

  1. Displays the root directory, which can be returned by entering the b character in any subdirectory
  2. Any subdirectory can return to the previous directory by entering the q character
  3. After the main directory enters the subdirectory, the system can print the subdirectory, enter the subdirectory or return it according to the specified print information

2, Code implementation

# Definition dictionary includes, country, state (province), city
dic = {'China': {'Hebei': ('Shijiazhuang', 'Xingtai', 'Hengshui',), 'Beijing': ('Changping', 'Haidian', 'Chaoyang',), 'Shanxi': ('Taiyuan', 'Da Tong', 'Yuncheng',)},
       'U.S.A': {'New York Prefecture': ('New York City', 'Rochester', 'buffalo',), 'California': ('Los Angeles', 'Hollywood', 'Alhambra',), 'Pennsylvania': ('Philadelphia', 'bethlehem', 'Washington',)},
       'Australia': {'Victoria': ('Melbourne', 'Geelong', 'Bali di',), 'Western Australia': ('magyar', 'Perth', 'Freeman Tu',), 'South Australia': ('Adelaide', 'landlocked', 'Baroa'), }}
# print(dic['China']['Hebei'])
dic_dir = {1: "China",
           2: "U.S.A",
           3: "Australia",
           }  # Define correspondence between country and serial number
dic_dirCity = {1: ("Hebei", "Beijing", "Shanxi"),
             2: ("New York Prefecture", "California", "Pennsylvania",),
             3: ("Victoria", "Western Australia","South Australia",)
             }# Province (state) correspondence

def Directory():#Define three level directory functions

    DirectoryPos = 0#Record current directory location
    while True:
        if DirectoryPos == 0:
            for temp in dic_dir:
                print(temp,":",dic_dir[temp])#Print root
            getCountry= int(input("Please Chonse The Country:"))#Conversion of input characters to int Type variables
            while getCountry<1 or getCountry>3:#Legitimacy judgment
                getCountry = int(input("Input Error Please Reinput:"))
            DirectoryPos +=1
        if DirectoryPos == 1:
            num = 0
            for temp in dic_dirCity[getCountry]:
                num += 1
                print(num,":",temp)#Print subdirectory
            getProvince = input("Please Chonse The Province\nInupt q Is Return\nInput b Back To Main:")
            if getProvince == 'q' or getProvince == 'b':#Condition judgment, enter the lower directory or return
                DirectoryPos -= 1
                DirectoryPos += 1
                getProvince = int(getProvince)
        if DirectoryPos == 2:
            for temp in dic[dic_dir[getCountry]] [dic_dirCity[getCountry][getProvince-1]]:#Print subdirectory
            getNum = input("Input q Is Return\nInput b Is Back To Main:")
            if getNum == 'q':#Conditional judgement
                DirectoryPos -= 1
            if getNum == 'b':
                DirectoryPos =0
#   print("The Game Is Over !")


The running result of the program is as follows:


This program has no special operating conditions. It can be used as long as it is copied and pasted to the version of PyCharm3.0 or above. It does not support the version of PyCharm3.0 or below. There are bug s in the validity judgment of the input conditions of this program that will not be handled temporarily due to time reasons. If there is time in the later stage, it will be updated in time. Interested friends can also try it on their own.

Keywords: Python

Added by voyde on Wed, 12 Feb 2020 17:31:32 +0200