上面的代碼實現的只是簡單而繁瑣的一種,後面的代碼會一直重覆,因此並沒有寫下去,後面的是我看視頻附帶的代碼,我也附上,較我寫的完整一些 ...
#實現省市級的三級菜單 #!/user/bin/env python citys={"廣東省":{"廣州市":["番禺區","天河區","白雲區"], "深圳市":["南山區","寶安區","羅湖區"], "惠州市":["惠東","惠陽","淡水"]}, "湖南省":{"武漢":["黃鶴樓","戶部巷","東湖"], "宜昌":["三峽大壩","葛洲壩","西峽"], "恩施":["神農溪","恩施土司城","女兒城"]}, "河北省":{"北京":["故宮","天安門","八達嶺"], "太原":["晉祠","柳巷","聖母殿"], "石家莊":["趙州橋","隆興寺","西柏波"]}} print(citys["廣東省"]["廣州市"]) while True: for a in citys.keys(): print(a) city=input("請輸入需要瞭解的城市:") if city in citys and city=="廣東省": for b in citys[city].keys(): print(b) area = input("請輸入需要瞭解的區域:") if area in citys[city] and area == "廣州市": for b in citys[city][area]: print(b) turn = input("按任意鍵返回上一層,按q退出程式") if turn == "q": break else: continue
上面的代碼實現的只是簡單而繁瑣的一種,後面的代碼會一直重覆,因此並沒有寫下去,後面的是我看視頻附帶的代碼,我也附上,較我寫的完整一些
data = { '北京':{ "昌平":{ "沙河":["oldboy","test"], "天通苑":["鏈家地產","我愛我家"] }, "朝陽":{ "望京":["賓士","陌陌"], "國貿":{"CICC","HP"}, "東直門":{"Advent","飛信"}, }, "海澱":{}, }, '山東':{ "德州":{}, "青島":{}, "濟南":{} }, '廣東':{ "東莞":{}, "常熟":{}, "佛山":{}, }, } exit_flag = False while not exit_flag: for i in data: print(i) choice = input("選擇進入1>>:") if choice in data: while not exit_flag: for i2 in data[choice]: print("\t",i2) choice2 = input("選擇進入2>>:") if choice2 in data[choice]: while not exit_flag: for i3 in data[choice][choice2]: print("\t\t", i3) choice3 = input("選擇進入3>>:") if choice3 in data[choice][choice2]: for i4 in data[choice][choice2][choice3]: print("\t\t",i4) choice4 = input("最後一層,按b返回>>:") if choice4 == "b": pass elif choice4 == "q": exit_flag = True if choice3 == "b": break elif choice3 == "q": exit_flag = True if choice2 == "b": break elif choice2 == "q": exit_flag = True
今天 2017/08/22,突然發現昨天的程式可以進行優化,可以實現每個省市級都進行選擇
#實現省市級的三級菜單 #!/user/bin/env python citys={"廣東省":{"廣州市":["番禺區","天河區","白雲區"], "深圳市":["南山區","寶安區","羅湖區"], "惠州市":["惠東","惠陽","淡水"]}, "湖南省":{"武漢":["黃鶴樓","戶部巷","東湖"], "宜昌":["三峽大壩","葛洲壩","西峽"], "恩施":["神農溪","恩施土司城","女兒城"]}, "河北省":{"北京":["故宮","天安門","八達嶺"], "太原":["晉祠","柳巷","聖母殿"], "石家莊":["趙州橋","隆興寺","西柏波"]}} print(citys["廣東省"]["廣州市"]) while True: for a in citys.keys(): print(a) city=input("請輸入需要瞭解的城市:") for b in citys[city].keys(): print(b) area = input("請輸入需要瞭解的區域:") for b in citys[city][area]: print(b) turn = input("按任意鍵返回上一層,按q退出程式") if turn == "q": break else: continue else: pass