python 三級菜單 while循環三次,湖北省市-縣-街道的選擇,3個while的循環 -day2
阿新 • • 發佈:2017-07-03
字典 utf ems Coding nbsp 上海 orm map []
python編寫一個三級while的循環菜單
1.定義字典,字典裏面嵌套字典,內嵌字典的值為列表。
思路:
湖北省的市:字典中的
定義3個字典,用於存儲
{序列-鍵:市名}
shiqu_dir = {}
{序列-鍵:縣}
xian_dir ={}
{序列-鍵:街道}
jiedao_dir = {}
函數將序列和鍵一一對應。
for number,key in enumerate(Hbei_map.keys()) 獲取序列鍵和市名存儲到 市名字典
用一個列表 chose = [] 來存儲所選擇的市--縣程-街道
chose[0],chose[1],chose[2]最後使用切片返回
中間輸入全部使用的判斷方式for循環: for k,v in xxx.items():
if input == k:
讓用戶只能輸入序列中的數字。使用判斷語句:
if int(input) not in 字典.keys():
print("您輸入了非法的序列,請輸入一下序列 {}".format(shiqu_list))
# -*- coding: utf-8 -*- __author__ = ‘hujianli‘ import sys Hbei_map = { "武漢市":{ "江岸區":["上海街","大智街","一元街","車站街","四唯街","永清街","球場街","西馬街","臺北街","View Code勞動街","花橋街","後湖街","諶家磯街"], "硚口區":["易家墩街道","韓家墩街道","宗關街道","漢水橋街道","寶豐街道","榮華街道","崇仁街道","漢中街道"], "武昌區":["積玉橋街道","楊園街道","徐家棚街道","新河街道"] }, "荊州市":{ "沙市區":["黃石港","崇文街道","解放路街道"], "荊州區":["城南街道","西城街道","東城街道","馬山鎮","川店鎮"], "松滋市":["新江口鎮","南海鎮","八寶鎮","陳店鎮"],"公安縣":["鬥湖堤鎮","埠河鎮","楊家廠鎮","斑竹垱鎮"], "監利縣":["容城鎮","白螺鎮","朱河鎮"] }, "孝感市":{ "孝南區":["肖港鎮","書院街道","車站街道","新華街道"], "應城市":["城中街道","城北街道","楊河鎮","三合鎮"], "安陸市":["李店鎮","巡店鎮","雷公鎮","陳店鄉"], }, } # print(Hbei_map["孝感市"]["應城市"]) shiqu_dir = {} xian_dir ={} jiedao_dir = {} def jiance(name): if len(name) == 0: sys.exit("\033[31;1m選擇不能為空,清重新輸入~~\033[0m") elif shiqu == "quit" or shiqu == "exit": sys.exit("\033[31;1m 退出程序,三級菜單退出~~ \033[0m") else: return True print("==============================================================================================================") print("湖北省下面的市區如下: ") for number,key in enumerate(Hbei_map.keys()): print(str(number+1) + ": " + str(key)) shiqu_dir[number+1] = key chose = [] shiqu_list = [x+1 for x in range(len(shiqu_dir.keys()))] while True: print("==========================================================================================================") shiqu = input("請輸入您要查詢的市區所對應的序列號,輸入exit或者quit自動會退出.:") jiance(shiqu) if int(shiqu) not in shiqu_dir.keys(): print("您輸入了非法的序列,請輸入一下序列 {}".format(shiqu_list)) else: for keys,vlaue in shiqu_dir.items(): if int(shiqu) == keys: chose = [] chose.append(vlaue) print("================================================================================================") print("該市區下面的縣級市或者片區域,縣城列表如下:") for number,keys_xian in enumerate(Hbei_map[vlaue].keys()): print(str(number+1)+ ":" + str(keys_xian)) xian_dir[number+1] = keys_xian while True: print("=============================================================================================") xian = input("請輸入要查詢的縣城所對應的序列號,quit/exit退出: ") jiance(xian) xian_list = [x+1 for x in range(len(xian_dir.keys()))] if int(xian) not in xian_dir.keys(): print("您輸入了非法的序列,請輸入一下序列 {}".format(xian_list)) else: for k,v in xian_dir.items(): if int(xian) == k: chose.append(v) jiedao_li = Hbei_map[chose[0]][chose[1]] for k,v in enumerate(jiedao_li): print(str(k+1) + ": " + str(v)) jiedao_dir[k+1] = v while True: print("=====================================================================================") jiedao = input("請輸入街道所對應的序列號 退出請輸入quit/exit: ") jiance(jiedao) jiedao_list = [x+1 for x in range(len(jiedao_dir.keys()))] if int(jiedao) not in jiedao_dir.keys(): print("您輸入了非法的序列,請輸入一下序列 {}".format(jiedao_list)) else: for k,v in jiedao_dir.items(): if int(jiedao) == k: chose.append(v) print("=================================================================================") print("\033[35;1m你選擇的市:%s 縣城是:%s 街道是:%s\033[0m"%(chose[0],chose[1],chose[2])) print("===================================================================================") break break break
python 三級菜單 while循環三次,湖北省市-縣-街道的選擇,3個while的循環 -day2