Week 1:作业2
#coding=utf-8 #Version:python3.6.0 #Tools:Pycharm 2017.3.2 __date__ = '2018/9/10 10:14' __author__ = 'Steven' ''' 作业三:多级菜单 三级菜单 可依次选择进入各子菜单 所需新知识点:列表、字典 ''' class Location(object): def __init__(self, index = 0xFF, name = '', level = 0xFF, parent_index = 0xFF, child_node_num = 0): self.index = index self.name = name self.level = level self.parent_index = parent_index self.child_node_num = child_node_num def _append_to_parent(self, par_node): if self.index == 0xFF or self.name == '': print("Error: imcomplete location property !") return 0xFF if self.level != par_node.level + 1: print("Error: Invalid parent node !") return 0xFF self.parent_index = par_node.index par_node.child_node_num += 1 return 0 effective_choice_flag = 0 loc_list = [[Location() for i in range(100)] for j in range(4)] #最多支持4级菜单,每级100个选项 cur_cls = 0xFF node_idx = [0, 0, 0, 0] with open('Menu_Data.txt', 'r', encoding='UTF-8') as menu_file_content: for line in menu_file_content: split_line = line.split() if int(split_line[0]) < 1 or int(split_line[0]) > 4: print("Fatal error: invalid class level !") effective_choice_flag = 0xFF break cur_cls = int(split_line[0]) - 1 cur_loc = Location(node_idx[cur_cls], str(split_line[1]), cur_cls) if cur_cls > 0: if 0xFF == cur_loc._append_to_parent(loc_list[cur_cls-1][node_idx[cur_cls-1]-1]): effective_choice_flag = 0xFF loc_list[cur_cls][node_idx[cur_cls]] = cur_loc node_idx[cur_cls] += 1 user_choice = [0xFF,0xFF, 0xFF, 0xFF] cls_name = ["Nation","Province/State", "City", "District"] print("Welcome to delivery system !") cur_cls = 0 while 1: if cur_cls >= 4: break print("Please choose the destination ", cls_name[cur_cls], " :") temp_idx = 0 effective_idx = [0xFF for i in range(100)] for cur_idx in range(node_idx[cur_cls]): if cur_cls > 0 and loc_list[cur_cls][cur_idx].parent_index != loc_list[cur_cls - 1][user_choice[cur_cls - 1]].index: continue effective_idx[temp_idx] = cur_idx print("{loc_id}.{loc_name}".format(loc_id = temp_idx, loc_name = loc_list[cur_cls][cur_idx].name)) temp_idx += 1 if temp_idx > 0: if cur_cls > 0: print("999.Return to the superior menu") cur_choice = int(input("Choose your location Id:")) if 999 == cur_choice: if cur_cls > 0: cur_cls -= 2 else: effective_choice_flag = 0xff elif cur_choice >=0 and cur_choice < temp_idx: user_choice[cur_cls] = effective_idx[cur_choice] else: print("Error: input out of range !") effective_choice_flag = 0xff break else: print("Oops, no effective option!") break cur_cls += 1 if 0 == effective_choice_flag: print("Please confirm: you have choosen {nation}{province}{city}{district} as your destination". format(nation = loc_list[0][user_choice[0]].name if user_choice[0] < 100 and loc_list[0][user_choice[0]].index != 0xFF else '', province = loc_list[1][user_choice[1]].name if user_choice[1] < 100 and loc_list[1][user_choice[2]].index != 0xFF else '', city = loc_list[2][user_choice[2]].name if user_choice[2] < 100 and loc_list[2][user_choice[2]].index != 0xFF else '', district =loc_list[3][user_choice[3]].name if user_choice[3] < 100 and loc_list[3][user_choice[3]].index != 0xFF else ''))
文件内容:
1 中国
2 上海市
3 浦东新区
3 徐汇区
3 杨浦区
2 广东省
3 深圳市
4 龙岗区
4 南山区
4 福田区
3 广州市
4 番禹区
4 白云区
4 花都区
1 美国
2 加利福利亚州
3 旧金山市
3 洛杉矶市
2 德克萨斯州
3 休斯顿市
3 奥斯汀市