实验3

import random
print('用列表存储随机整数: ')
ls = [random.randint(1, 100) for i in range(5)]
print(ls)
print('\n用集合存储随机整数: ')
s1 = {random.randint(1,100) for i in range(5)}
print(s1)
print('\n用集合存储随机整数: ')
s2 = set()
while(len(s2)<5):
   s2.add(random.randint(1,100))
print(s2)

 

 

 问题1: random.randint(1,100) 生成的随机整数范围是?能否取到100?

1-99 不能

 

问题2:利用 list(range(5)) 生成的有序序列范围是?是否包括5?利用 list(range(1,5)) 生成的有序序列范围是?是否包括5?

0-4 不包括5   1-4 不包括5

 

问题3:使用line8生成的集合s1,len(s1)一定是5吗?

不一定是

问题4:使用line12-14生成的集合s2,len(s2)一定是5吗?

一定是

datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
'2049777002': ['音乐', '旅行'],
'2049777003': ['马拉松', '健身', '游戏'],
'2049777004': [],
'2049777005': ['足球', '阅读'],
'2049777006': ['发呆', '闲逛'],
'2049777007': [],
'2049777008': ['书法', '电影'],
'2049777009': ['音乐', '阅读', '电影', '漫画'],
'2049777010': ['数学', '推理', '音乐', '旅行']
}
number_youxi=0
number_yinyue=0
number_dianying=0
number_manhua=0
number_zuqiu=0
number_tuili=0
number_shuxue=0
number_yuedu=0
number_lvxing=0
number_shufa=0
number_malasong=0
number_jianshen=0
number_lanqiu=0
number_yumaoqiu=0
number_fadai=0
number_xianguang=0
number_meishi=0
list=list(dict.values(datas))
for i in range(10):
    x=list[i]
    for x1 in range(len(x)):
        x2=x[x1]
        while True:
           if '音乐' in x2:
               number_yinyue+=1
           elif '电影' in x2:
               number_dianying+=1
           elif '漫画' in x2:
               number_manhua+=1
           elif '阅读' in x2:
               number_yuedu+=1
           elif '健身' in x2:
               number_jianshen += 1
           elif '数学' in x2:
               number_shuxue+=1
           elif '书法' in x2:
               number_shufa+=1
           elif '旅行' in x2:
               number_lvxing+=1
           elif '游戏' in x2:
               number_youxi+=1
           elif '推理' in x2:
               number_tuili+=1
           elif '足球' in x2:
               number_zuqiu += 1
           elif '马拉松' in x2:
               number_malasong+=1
           elif '篮球' in x2:
               number_lanqiu+=1
           elif '羽毛球' in x2:
               number_yumaoqiu+=1
           elif '发呆' in x2:
               number_fadai+=1
           elif '闲逛' in x2:
               number_xianguang=1
           elif '美食' in x2:
               number_meishi+=1
           break
print('音乐:',number_yinyue)
print('漫画:',number_manhua)
print('旅行:',number_lvxing)
print('阅读:',number_yuedu)
print('电影:',number_dianying)
print('篮球:',number_lanqiu)
print('羽毛球:',number_yumaoqiu)
print('美食:',number_meishi)
print('马拉松:',number_malasong)
print('健身:',number_jianshen)
print('游戏:',number_youxi)
print('足球:',number_zuqiu)
print('闲逛:',number_xianguang)
print('发呆:',number_fadai)
print('书法:',number_shufa)
print('数学:',number_shuxue)
print('推理:',number_tuili)

 

 

temperatures= [['2022-04-18', '星期一', '10', '22'],
['2022-04-19', '星期二', '10', '22'],
['2022-04-20', '星期三', '12', '25'],
['2022-04-21', '星期四', '13', '28'],
['2022-04-22', '星期五', '16', '31'],
['2022-04-23', '星期六', '19', '31'],
['2022-04-24', '星期日', '19', '27']]
list=[]
for i in range(7):
    x=temperatures[i]
    y=x[2]
    list.append(y)
list1=[]
for o in range(7):
    c=temperatures[o]
    v=c[3]
    list1.append(v)
print('未来一周最低气温:',min(list))
print('未来一周最高气温:',max(list1))
list=[int(p) for p in list]
list1=[int(p1) for p1 in list1]
x1=sum(list)
x2=sum(list1)
print(f'未来一周平均气温区间:{int(x1/7)}-{int(x2/7)}')

 

 

 


import random
day=random.randint(1,31)
for i in range(3):
x=input('猜猜2022年5月几日是你的lucky day:-')
i+=1
if i==3 and day!=int(x):
print('次数用光了')
print('偷偷告诉你 你的lucky day 是',day)
while True:
if day == int(x) :
print('哇,猜中了')
elif int(x) < day and i !=3:
print('猜早了,你的lucky day 还没到呢')
elif int(x) > day and i!=3:
print('猜晚了,你的lucky day 已经过了')
break
 

 

 

 

 

num, n = eval(input("输入十进制整数x和要转换的进制n(2或8或16):"))
a = []
shengxia = num//n
while shengxia :
    a.append(shengxia%n)   
    shengxia= shengxia//n      
a.append(num%n)
x=len(a)
alpha=['A','B','C','D','E','F']
if a[x-1]==10:
    a[x-1]=alpha[0]
if a[x-1]==11:
    a[x-1]=alpha[1]
if a[x-1]==12:
    a[x-1]=alpha[2]
if a[x-1]==13:
    a[x-1]=alpha[3]
if a[x-1]==14:
    a[x-1]=alpha[4]
if a[x-1]==15:
    a[x-1]=alpha[5]
for i in a[::1]:
    print(i,end='')

 

 

 

 

 

posted @ 2022-04-20 18:56  戎梓瑞  阅读(16)  评论(2编辑  收藏  举报