First, the entire case to apply the knowledge point
Use of Dictionary of 1.python data structure, dictionary nested dictionary, dictionary nested list
Use of list of 2.PYTHON data structures, dictionary nesting list
The use of strings for 3.python data structures, formatting of strings
4.while true Dead loop usage
5.if...else .... Use of statements
Second, the core idea of case design
1. Store city information in a dictionary + list of data structures inside
2. Set the province, city, and first to the level three menu
3. According to the user's choice can choose to enter each sub-menu
Third, the Code
#!/usr/bin/env python# -*- coding:utf-8 -*-"" "message_dict = { "Sichuan": {"Guang ' an": ["Wusheng", "Yuechi", "Linshui"], "Guangyuan": ["Wangcang", "Cangxi", "Guangyuan urban area"]}, "Beijing": {"Haidian": ["Zhongguancun", "Five crossings", "land "]," Chaoyang ": [" Chang Ying "," Guomao "," Guanzhuang "]}}print message_dict.keys ()" "" #使用字典和列表村村省, city, town and other information message_dict = { "Sichuan":{ "Guangan": ["Wusheng", "Yuechi", " Linshui "], " Guangyuan ": [" Wangcang "," Cangxi "] }, "Beijing":{ "Chaoyang": [" Changying "," Guanzhuang "," Guomao "], " Haidian ": [" Zhongguancun "," Wudaokou "," Shangdi "]    }} #打印显示所有的省份for i in range (Len (Message_dict.keys ()) ): new_item1 = "%s:%s" % (I+1,message_dict.keys () [i]) print new_item1# requires the user to select which municipalities are under the province to view and to ensure that the user enters the correct province provence = raw_input ("Plz input select province : ") While true: if provence not in message_dict.keys (): print "Plz input correct!!!" provence = raw_input ("Plz input select province again: ") else: Breakmessage_dict1 = message_dict[provence] #显示对应省份的所有市区的信息for j in range (Len (message_ Dict1): new_item2 = "%s:%s" % (J+1,message_dict1.keys () [j]) print new_item2# asks the user to enter which towns in the urban area to view and to ensure that the user enters the correct city = raw_input ("plz select City: ") While true: if city not in message_dict1.keys (): print " Plz input correct!!! " city = raw_input ("plz select city Again: ") else: breakmessage_list2 = message_dict1[city] #打印用户所要查看的市区的的所有城镇for k in range (len (message_list2)): new_item3 = "%s:%s" % (K+1,message_list2[k]) print new_ Item3
Python Learning Path Case 3--Multi-level menu show City details