实验三

3.1
1
import random 2 print('用列表存储随机整数:') 3 Is=[random.randint(1,100)for i in range(5)] 4 print(Is) 5 6 print('\n用集合存储随机整数:') 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.范围是[1,100] ,能
2.范围是[0,5),不包括。[1,5),不包括
3.不一定
4.一定是

3.2

 1 datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
 2         '2049777002': ['音乐', '旅行'],
 3         '2049777003': ['马拉松', '健身', '游戏'],
 4         '2049777004': [],
 5         '2049777005': ['足球', '阅读'],
 6         '2049777006': ['发呆', '闲逛'],
 7         '2049777007': [],
 8         '2049777008': ['书法', '电影'],
 9         '2049777009': ['音乐', '阅读', '电影', '漫画'],
10         '2049777010': ['数学', '推理', '音乐', '旅行']
11         }
12 a=list(datas.values())
13 b=[]
14 c={}
15 for m in a:
16     for n in m:
17         b.append(n)
18 c=c.fromkeys(b,0)
19 for i in c:
20     c[i]=b.count(i)
21 c=sorted([(v,k) for k,v in c.items()],reverse=True)
22 for v,k in c:
23     print(f'{k}:{v}')

 

 3.3

 

 1 temperatures= [['2022-04-18', '星期一', '10', '22'],
 2                ['2022-04-19', '星期二', '10', '22'],
 3                ['2022-04-20', '星期三', '12', '25'],
 4                ['2022-04-21', '星期四', '13', '28'],
 5                ['2022-04-22', '星期五', '16', '31'],
 6                ['2022-04-23', '星期六', '19', '31'],
 7                ['2022-04-24', '星期日', '19', '27']]
 8 a=[];b=[]
 9 for i in temperatures:
10     a.append(int(i[2]));b.append(int(i[3]))
11 s1=0;s2=0
12 for i in range(len(a)):
13     s1+=a[i];s2+=b[i]
14 a1=int(s1/len(a));a2=int(s2/len(b))
15 print(f'未来一周最低气温:{min(a)}°C\n未来一周最高气温:{max(b)}°C\n未来一周平均气温区间{a1}~{a2}°C')

 

 3.4

 1 import random
 2 ld=random.randint(1,31)
 3 print('猜猜2022年5月哪一天会是你的lucky day:-)')
 4 cishu=0
 5 for i in range(3):
 6     a=eval(input('你有三次机会,猜吧(1~31):'))
 7     if a==ld:
 8         print('哇,猜中了:-)')
 9         break
10     elif a>ld:
11         print('猜晚啦,你的lucky day已经过啦')
12     elif a<ld:
13         print('猜早啦,你的lucky day还没到呢')
14     cishu+=1
15 if cishu==3:
16     print(f'次数用光啦。\n偷偷告诉你,5月你的lucky day是{ld}号,good luck:-)')
17     

 

 3.5

 

 1 x,n=eval(input('输入十进制整数x和要转换的进制n(2或8或16):'))
 2 b=x
 3 i=[]
 4 m={10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
 5 if n==2:
 6     while x>=1:
 7         a=int(x%n)
 8         i.append(a)
 9         x=(x-a)/n
10 elif n==8:
11     while x>=1:
12         a=int(x%n)
13         i.append(a)
14         x=(x-a)/n
15 elif n==16:
16     while x>=1:
17         a=int(x%n)
18         if a>=10:
19             i.append(m[a])
20         else:
21             i.append(a)
22         x=(x-a)/n
23 i.reverse()
24 print(f'{b}的{n}进制是:',end='')
25 for m in i:
26     print(m,end='')

 

 

 

 

posted on 2022-04-23 13:41  VolcanX  阅读(14)  评论(3编辑  收藏  举报