Multi-level menu
阿新 • • 發佈:2017-07-22
pri ict lines readline () python open str div
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import sys #全局列表 dic_with_num = {} dic_with_chinese = {} #字典導入 def dic_import (): with open(‘dic.txt‘,‘r+‘) as file: file_list = file.readlines() dic= {} dic1 = {} dic2 = {} dic3 = {} i = 1 j = 1 k= 1 for line in file_list : (key1,key2) = line.strip(‘\n‘).split() if key2 == ‘省‘: i = 1 j = 1 line1 = key1 dic1[line1]=dic3 dic3 = {} dic2[str(k)] = dic1 dic1= {} dic = dict(dic,**dic2) dic2 ={} k +=1 elif key2 == ‘市‘: i = 1 line2 = key1 dic2[line2]=dic1 dic1={} dic1[str(j)]=dic2 dic2={} dic3=dict(dic3,**dic1) dic1={} j +=1 else: line3 = key1 dic1[str(i)] = line3 i +=1 return dic #字典交換-當用戶輸入中文時,轉換字典 def dic_exchange (): dic_with_chinese = {} dic = {} dic1 = {} for line1 in dic_with_num.keys(): for line2 in dic_with_num[line1].keys(): for line3 in dic_with_num[line1][line2].keys(): for line4 in dic_with_num[line1][line2][line3].keys(): for line5,line6 in dic_with_num[line1][line2][line3][line4].items(): dic[line6] = line5 dic1[line3] = dic dic = {} dic[line4] = dic1 dic_with_chinese = dict(dic_with_chinese,**dic) dic = {} dic1 ={} dic[line1] = dic_with_chinese dic_with_chinese = {} dic_with_chinese[line2] = dic dic1 = dict(dic1,**dic_with_chinese) dic = {} dic_with_chinese = {} return dic1 #初始化 dic_with_num = dic_import() dic_with_chinese = dic_exchange() while True: #省份顯示 for k in dic_with_num.keys(): for i in dic_with_num[k].keys(): print(k,i + ‘\t‘, end= ‘‘) if int(k)%4 ==0: print(‘\n‘) #詢問用戶需要查詢的省份,可輸入中文 province = input(‘\n請輸入需要查詢的省份\n‘).strip() #城市顯示 #用戶輸入為序號時 if province.isdigit(): if province not in dic_with_num.keys(): print(‘選項不存在\n‘) continue else: province_chinese = list(dic_with_num[province].keys())[0] #獲取序號對應的省份中文名 for k in dic_with_num[province][province_chinese].keys(): for i in dic_with_num[province][province_chinese][k].keys(): print(k,i + ‘\t‘ ,end = ‘‘) if int(k) % 4 == 0: print(‘\n‘) #用戶輸入中文 else: if province not in dic_with_chinese.keys(): print(‘選項不存在\n‘) continue else: province_chinese = list(dic_with_chinese[province].keys())[0] #獲取省份對應的序號 for k in dic_with_chinese[province][province_chinese].keys(): for i in dic_with_chinese[province][province_chinese][k].keys(): print(i,k + ‘\t‘,end=‘‘) if int(i) % 4 == 0: print(‘\n‘) #詢問用戶需要查詢的城市,可輸入中文 while True: city = input(‘請輸入需要查詢的城市\n‘) #用戶輸入為序號時 if city.isdigit(): if city not in dic_with_num[province][province_chinese].keys(): print(‘選項不存在\n‘) continue else: city_chinese = list(dic_with_num[province][province_chinese][city].keys())[0]#獲取序號對應的城市中文名 for k ,v in dic_with_num[province][province_chinese][city][city_chinese].items(): print(k,v + ‘\t‘,end=‘‘) if int(k) % 4 == 0: print(‘\n‘) choice = input(‘\n可輸入q返回上一層菜單\n輸入m返回主菜單\n輸入其他則退出該系統‘) if choice == ‘q‘: province_chinese = list(dic_with_num[province].keys())[0] # 獲取序號對應的省份中文名 for k in dic_with_num[province][province_chinese].keys(): for i in dic_with_num[province][province_chinese][k].keys(): print(k, i + ‘\t‘ ,end=‘‘) if int(k) % 4 == 0: print(‘\n‘) continue elif choice == ‘m‘: break else: sys.exit() #用戶輸入q時 返回省份顯示菜單 elif city == ‘q‘: break #用戶輸入為中文時 else: if city not in dic_with_chinese[province][province_chinese].keys(): print(‘選項不存在\n‘) continue else: city_chinese = list(dic_with_chinese[province][province_chinese][city].keys())[0] # 獲取序號對應的城市中文名 for k, v in dic_with_chinese[province][province_chinese][city][city_chinese].items(): print(v, k) choice = input(‘‘‘ ======================= **可輸入q返回上一層菜單** >輸入m返回主菜單< >輸入其他則退出該系統< ======================= ‘‘‘) if choice == ‘q‘: province_chinese = list(dic_with_chinese[province].keys())[0] # 獲取省份對應的序號 for k in dic_with_chinese[province][province_chinese].keys(): for i in dic_with_chinese[province][province_chinese][k].keys(): print(i, k) continue elif choice == ‘m‘: break else: sys.exit()
Multi-level menu