主要是练习while 循环和 if 条件判断的使用
#!_*_coding:utf-8_*_ # Author: hkey def options(list): for i, v in enumerate(list): print(i + 1, v) choice = input('33[34;1m请选择:33[0m') return choice def city(list): while True: choice = options(list) if choice == str(len(list)): break def start(): while True: print('33[32;1m欢迎查询省份城市33[0m'.center(30, '#')) choice = options(prov_list) if choice == '1': city(sx) elif choice == '2': city(shanxi) elif choice == '3': city(sh) if choice == '4': break if __name__ == '__main__': prov_list = ['陕西省', '山西省', '上海市', '退出'] sx = ['西安市', '安康市', '咸阳市', '返回'] shanxi = ['太原市', '运城市', '返回'] sh = ['上海市', '返回'] start()
另一个版本:
# -*- coding: utf-8 -*- prov_list = ['陕西省', '浙江省', '退出'] sx_city_list = ['西安市', '咸阳市', '宝鸡市', '返回'] zj_city_list = ['杭州市', '宁波市', '绍兴市', '返回'] while True: for k, v in enumerate(prov_list): print(k+1, v) choice = input('33[34;1m请选择:33[1m') if choice == '1': while True: for k, v in enumerate(sx_city_list): print(k+1, v) choice = input('33[34;1m请选择:33[1m') if choice == str(len(sx_city_list)): break elif choice == '2': while True: for k, v in enumerate(zj_city_list): print(k+1, v) choice = input('33[34;1m请选择:33[1m') if choice == str(len(zj_city_list)): break elif choice == '3': break else: print('输入序号不存在,请重新输入!')