Python dictionary provides a simple three-level menu (for example ),
As follows:
Data = {"Beijing": {"Changping": {"shahe": ["oldboy", "test"], "tiantongyuan": ["linked property ", "I love my family"]}, "Chaoyang": {"Wangjing": ["Benz", "momo"], "International trade": ["CICC", "HP"], "Dongzhimen": ["advent", "Feixin"]}, "Haidian" :{}}, "Shandong": {"Dezhou" :{}, "Qingdao ": {}, "Jinan" :{}, "Guangdong": {"Dongguan" :{}, "Changshu" :{}, "Foshan ": {}}} exit_flag = Falsewhile not exit_flag: for I in data: print (I) choice_s = input ("enter the name of any of the preceding provinces or municipalities >> :") if choice_s in data: while not exit_flag: for I in data [choice_s]: print ("\ t", I) choice_q = input ("enter the name of any of the above cities >>:") if choice_q in data [choice_s]: while not exit_flag: for I in data [choice_s] [choice_q]: print ("\ t", I) choice_j = input ("enter the name of any of the preceding streets >>:") if choice_j in data [choice_s] [choice_q]: for I in data [choice_s] [choice_q] [choice_j]: print ("\ t", I) last = input ("last layer, return by B >>>: ") if last =" B ": pass elif last =" q ": exit_flag = True if choice_j =" B ": break elif choice_j =" q ": exit_flag = True if choice_q = "B": break elif choice_q = "q": exit_flag = True
Key points:
1. hierarchical relationship of dictionaries
2. break terminates this loop, and pass does nothing. It is a placeholder.
3. cleverly use exit_flag = False
The above Python dictionary implements a simple three-level menu (for example), which is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support to the customer's house.