1 import random 2 print('用列表存储随机整数: ') 3 ls = [random.randint(1, 100) for i in range(5)] 4 print(ls) 5 print('\n用集合存储随机整数: ') 6 7 s1 = {random.randint(1,100) for i in range(5)} 8 print(s1) 9 10 print('\n用集合存储随机整数: ') 11 s2 = set() 12 while(len(s2)<5): 13 s2.add(random.randint(1,100)) 14 print(s2)
问题一:1~100,能取到100
问题二:0~4,不包括5
问题三:不一定,随机生成的数可能相等,s1是集合,没有相同元素,所以可能小于5
问题四:一定,因为当len(s2)小于5时循环会继续。
1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'], 2 '2049777002': ['音乐', '旅行'], 3 '2049777003': ['马拉松', '健身', '游戏'], 4 '2049777004': [], 5 '2049777005': ['足球', '阅读'], 6 '2049777006': ['发呆', '闲逛'], 7 '2049777007': [], 8 '2049777008': ['书法', '电影'], 9 '2049777009': ['音乐', '阅读', '电影', '漫画'], 10 '2049777010': ['数学', '推理', '音乐', '旅行'] 11 } 12 13 hobby = [] 14 time = [] 15 16 list1 = [] 17 18 for i in list(datas.values()): 19 list1 += i 20 21 for j in list1: 22 if j not in hobby: 23 hobby.append(j) 24 time.append(list1.count(j)) 25 26 result = zip(time,hobby) 27 result = sorted(result,reverse=True) 28 29 for value,key in result: 30 print(f'{key}:{value}')
1 high = 0 2 for i in range(len(temperatures)): 3 low += int(temperatures[i][2]) 4 high += int(temperatures[i][3]) 5 if i ==0: 6 lowest = int(temperatures[i][2]) 7 highest = int(temperatures[i][3]) 8 if int(temperatures[i][2]) < lowest: 9 lowest = int(temperatures[i][2]) 10 if int(temperatures[i][3]) > highest: 11 highest = int(temperatures[i][3]) 12 average_low = low/len(temperatures) 13 average_high = high/len(temperatures) 14 15 print(f'未来一周最低气温:{lowest}℃') 16 print(f'未来一周最低气温:{highest}℃') 17 print(f'未来一周平均气温区间:{average_low:.0f}℃~{average_high:.0f}℃')
1 import random 2 luck_day = random.randint(1,31) 3 4 print('猜猜2022年5月那一天会是你的lucky day:-)') 5 for i in range(3): 6 gess_day = eval(input('你有三次机会,猜吧(1~31):')) 7 if gess_day == luck_day: 8 print('哇,猜中了:-)') 9 break 10 elif gess_day < luck_day: 11 print('猜早啦,你的lucky day还没到呢') 12 elif gess_day > luck_day: 13 print('猜晚啦,你的lucky day已经过啦') 14 else: 15 print(f'次数用光了,\n偷偷告诉你,5月你的lucky day是{luck_day}号.good lucky:-)')
1 x,n = eval(input('输入十进制整数x和要转换的进制n(2或8或16):')) 2 x1 = x 3 x_list = [] 4 while x1 > 0: 5 x1_tuple = divmod(x1, n) 6 if x1_tuple[1]>=10: 7 x1_dect = {10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'} 8 x_list.append(x1_dect[x1_tuple[1]]) 9 else: 10 x_list.append(str(x1_tuple[1])) 11 x1 = x1_tuple[0] 12 13 x_list.reverse() 14 15 print(f'{x}的{n}进制是:{"".join(x_list)}')