Multi-level menu display in PYTHON dictionary Mode
The following figure shows a multi-level menu written in the python dictionary and for loop mode: the code implementation method is as follows:
#! /Usr/bin/env python #-*-coding: UTF-8-*-# __author _ = 'vector' dict = {"Heilongjiang": {"Harbin ": {"founder county": ['tianmen township ', 'baoxing township'], "Bin County": ['yonghe township ', 'minghe township'], "Mulan county ": ["Jixing township", "jiguo township"],}, "Daqing": {"zhazhou county": ["Fuxing township", "haode Mongolian township"], "Lindian county ": ['sanhexiang ', 'liming township'] },}, "Shanxi": {"Taiyuan": {"qingxu county": ['xigu township ', "jiyi township"], "yangqu county": ['lingjing Dian Xiang ', 'xiling Jing Xiang']}, "Shuozhou": {"Yingxian": ["nanquan township", "xiamayu township"], "huairen county ": ['hometown of affinity ', 'hometown of New home'] },}}exit_flag = Falsenum = 0 while not exit_flag: for index, key in enumerate (dict. keys (): num + = 1 print "\ 033 [32 m --> \ 033 [0 m", index, key a_1 = num = 0 choice_1 = raw_input ("\ 033 [34 mPlease your num: \ 033 [0 m "). strip () if not choice_1.isdigit (): if choice_1 = 'quit ': exit_flag = True print "\ 033 [31 m <------> going to quit <-----> \ 033 [0 m" else: print "\ 033 [31m please enter the correct number, \ 033 [0 m" else: choice_1 = int (choice_1) if choice_1> = a_1: print "\ 033 [31m sorry, please enter the correct number, and \ 033 [0 m" elif choice_1 <a_1: key_1 = dict. keys () [choice_1] num = 0 while not exit_flag: for index, key in enumerate (dict [key_1]): num + = 1 print "\ 033 [32 m ----> \ 033 [0 m", index, key a_2 = num = 0 choice_2 = raw_input ("\ 033 [34 mPlease your num1: \ 033 [0 m "). strip () if not choice_2.isdigit (): if choice_2 = 'back': break elif choice_2 = 'quit ': exit_flag = True print "\ 033 [31 m <------> going to quit <-----> \ 033 [0 m" break else: choice_2 = int (choice_2) if choice_2> = a_2: print "\ 033 [31m sorry, please enter the correct number, and \ 033 [0 m" elif choice_2 <a_2: key_2 = dict [key_1]. keys () [choice_2] while not exit_flag: for index, key in enumerate (dict [key_1] [key_2]): num + = 1 print "\ 033 [32 m ------> \ 033 [0 m", index, key a_3 = num = 0 choice_3 = raw_input ("\ 033 [34 mPlease your num2: \ 033 [0 m "). strip () if not choice_3.isdigit (): if choice_3 = 'quit ': exit_flag = True print "\ 033 [31 m <------> going to quit <-----> \ 033 [0 m" break if choice_3 = 'back': break else: choice_3 = int (choice_3) if choice_3> = a_3: print "\ 033 [31m sorry, please enter the correct number, \ 033 [0 m" elif choice_3 <a_3: key_3 = dict [key_1] [key_2]. keys () [choice_3] while not exit_flag: for index, key in enumerate (dict [key_1] [key_2] [key_3]): print "\ 033 [32 m --------> \ 033 [0 m", index, key choice_4 = raw_input ("\ 033 [34 mPlease your num3: \ 033 [0 m "). strip () if choice_4.isdigit (): print "\ 033 [31 m <is already the last layer,> \ 033 [0 m" elif choice_4 = 'quit ': exit_flag = True elif choice_4 = 'back': break else: print "\ 033 [31 m <------> going to quit <-----> \ 033 [0 m"