退出程式不用exit() #01代碼 1 #__author: _nbloser 2 #date: 2017/12/5 3 4 5 shaoguan = ['仁化', '始興', '樂昌', '南雄'] 6 jiangmeng = ['開平', '蓬江', '台山', '鶴山', '恩平'] 7 g ...
退出程式不用exit()
#01初級代碼
1 #__author: _nbloser 2 #date: 2017/12/5 3 4 5 shaoguan = ['仁化', '始興', '樂昌', '南雄'] 6 jiangmeng = ['開平', '蓬江', '台山', '鶴山', '恩平'] 7 guangdong_province = {'韶關':shaoguan, '江門':jiangmeng} 8 9 nanchang = ['東湖', '西湖', '新建', '安義', '進賢'] 10 jingdezhen = ['昌江', '珠山', '樂平', '浮梁'] 11 jiangxi_province = {'南昌':nanchang, '景德鎮':jingdezhen} 12 13 provinces = {'廣東':guangdong_province, '江西':jiangxi_province} 14 15 def pr(array): 16 for i in array: 17 print(i, end=" ") 18 print() 19 20 while True: 21 print("省份:", end="") 22 name_province = list(provinces.keys()) 23 #print(name_province) 24 pr(name_province) 25 pro_input = input("輸入你要查看的省份/輸入exit退出:") 26 if pro_input == 'exit': 27 exit() 28 if pro_input not in pro_input: 29 print("沒有該省份!") 30 continue 31 while True: 32 tmp_province = provinces[pro_input] 33 name_shis = list(tmp_province.keys()) 34 #print(tmp_province) 35 pr(name_shis) 36 shi_input = input("輸入你要查看的市/輸入exit退出/輸入return返回上一層:") 37 if shi_input == 'exit': 38 exit() 39 if shi_input == 'return': 40 break 41 if shi_input not in tmp_province: 42 print("沒有該市!") 43 continue 44 while True: 45 tmp_county = tmp_province[shi_input] 46 pr(tmp_county) 47 county_input = input("輸入exit退出/輸入return返回上一層:") 48 if county_input == 'exit': 49 exit() 50 if county_input == 'return': 51 breakView Code
#02plus_code
1 #__author: _nbloser 2 #date: 2017/12/5 3 4 #plus 5 shaoguan = ['仁化', '始興', '樂昌', '南雄'] 6 jiangmeng = ['開平', '蓬江', '台山', '鶴山', '恩平'] 7 guangdong_province = {'韶關':shaoguan, '江門':jiangmeng} 8 9 nanchang = ['東湖', '西湖', '新建', '安義', '進賢'] 10 jingdezhen = ['昌江', '珠山', '樂平', '浮梁'] 11 jiangxi_province = {'南昌':nanchang, '景德鎮':jingdezhen} 12 13 provinces = {'廣東': guangdong_province, '江西': jiangxi_province} 14 15 def pr(array): 16 for i in array: 17 print(i, end=" ") 18 print() 19 20 back_flag = False 21 exit_flag = False 22 23 while not exit_flag and not back_flag: 24 print("省份:", end="") 25 name_province = list(provinces.keys()) 26 pr(name_province) 27 pro_input = input("輸入你要查看的省份/輸入q或b退出:") 28 if pro_input == 'q': 29 exit_flag = True 30 if pro_input == 'b': 31 back_flag = True 32 if pro_input in provinces: 33 while not exit_flag and not back_flag: 34 tmp_province = provinces[pro_input] 35 name_shis = list(tmp_province.keys()) 36 pr(name_shis) 37 shi_input = input("輸入你要查看的市/輸入q退出/輸入b返回上一層:") 38 if shi_input == 'q': 39 exit_flag = True 40 if shi_input == 'b': 41 back_flag = True 42 if shi_input in tmp_province: 43 while not exit_flag and not back_flag: 44 tmp_county = tmp_province[shi_input] 45 pr(tmp_county) 46 county_input = input("輸入q退出/輸入b返回上一層:") 47 if county_input == 'q': 48 exit_flag = True 49 if county_input == 'b': 50 back_flag = True 51 else: 52 back_flag = False 53 else: 54 back_flag = FalseView Code
#03高大上code
1 #__author: _nbloser 2 #date: 2017/12/7 3 #double puls 4 shaoguan = ['仁化', '始興', '樂昌', '南雄'] 5 jiangmeng = ['開平', '蓬江', '台山', '鶴山', '恩平'] 6 guangdong_province = {'韶關':shaoguan, '江門':jiangmeng} 7 8 nanchang = ['東湖', '西湖', '新建', '安義', '進賢'] 9 jingdezhen = ['昌江', '珠山', '樂平', '浮梁'] 10 jiangxi_province = {'南昌': nanchang, '景德鎮': jingdezhen} 11 12 provinces = {'廣東': guangdong_province, '江西': jiangxi_province} 13 14 current_layer = provinces 15 parent_layer = [] 16 exit_flag = False 17 while not exit_flag: 18 for key in current_layer: 19 print(key, end=" ") 20 print() 21 choice = input(">>") 22 if choice in current_layer and type(current_layer) != list: 23 parent_layer.append(current_layer) 24 current_layer = current_layer[choice] 25 elif choice == 'b': 26 if parent_layer: 27 current_layer = parent_layer.pop() 28 else: break 29 elif choice == 'q': 30 exit_flag = True 31 else: 32 print("查無此項/為最底層!")View Code