import random chose_project = {1: '双色球', 2: '号码', 3: '天气', 0: '退出'} # 验证输入 def check_num(num): if num.isdigit(): num = int(num) return num else: print('请输入数字') # 双色球 def generate_lottery(num): for j in range(0, int(num)): lst1 = [] # 红球会出现重复数字 # for i in range(0, 6): # n = random.randint(1,33) # lst1.append(n) # n = random.randint(1,16) # lst1.append(n) # 红球不会出现重复数字 nums_lst = random.sample(range(1, 33), 6) lst1.extend(nums_lst) nums_lst = random.randint(1, 16) lst1.append(nums_lst) print(lst1) phone_numbers_str = '匪警[110],火警[119],急救[120]' # 号码 def find_phone(keyword): phone_lst = phone_numbers_str.split(',') result_lst = [] for p in phone_lst: # if num in p: # result_lst.append(p) if p.find(keyword) != -1: result_lst.append(p) return result_lst weather_str = '北京,晴天,1℃=上海,下雨,4℃=广州,下雨,4℃' # 天气 def get_weather(city): weather_dict = {} weather_lst = weather_str.split('=') for i in range(0, len(weather_lst)): w = weather_lst[i].split(',') weather = {'name': w[0], 'weather': w[1], 'max': w[2]} weather_dict[weather['name']] = weather if city in weather_dict: return weather_dict.get(city) else: return {} while True: for k, v in chose_project.items(): print(k, v) n = input('输入:') n = check_num(n) if n == 1: n = input('随机几组号码:') generate_lottery(n) elif n == 2: n = input('输入要查询的机构或号码:') result = find_phone(keyword=n) if result: # print(result) for phone_num in result: print(phone_num) else: print('查询不到') elif n == 3: n = input('输入要查询天气的城市:') result = get_weather(n) if result: print('{name},{weather},{max}'.format_map(result)) else: print('暂无{}天气'.format(n)) elif n == 0: print('退出') print('—' * 36) break else: print('请重新输入') print('—' * 36) print('感谢使用!')