编程语言
首页 > 编程语言> > 实验3:控制语句与组合数据类型应用编程

实验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)
datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
'2049777002': ['音乐', '旅行'],
'2049777003': ['马拉松', '健身', '游戏'],
'2049777004': [],
'2049777005': ['足球', '阅读'],
'2049777006': ['发呆', '闲逛'],
'2049777007': [],
'2049777008': ['书法', '电影'],
'2049777009': ['音乐', '阅读', '电影', '漫画'],
'2049777010': ['数学', '推理', '音乐', '旅行']}
list1=list(datas.values())
s=set()
list2=[]
print(list1)
for i in list1:
    for b in i:
        s.add(b)
for j in s:
    a=0
    for i in list1:
        for b in i:
            if j==b:
                a+=1
    list2.append(a)
d2=dict(zip(s,list2))
l1=list(d2.items())
l2=sorted(l1,key=lambda i:i[1],reverse=True)
for m,n in l2:
    print(f'{m},{n}')

temperaturetemperatures= [['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']]
list1 = []
list2 = []
x=0
y=0
for i in temperatures:
    list1.append(int(i[2]))
    list2.append(int(i[3]))
for s in list1:
    x=x+s
x=x//len(list1)
for t in list2:
    y=y+t
y=y//len(list2)
print('未来一周最低气温:',min(list1),'℃')
print('未来一周最高气温:',max(list2),'℃')
print('未来一周平均气温区间:',x,'℃ ~ ',y,'℃')

import random
x = random.randint(1,31)
print('猜猜2022年5月哪天是你的lucky day')
y = eval(input('你有三次机会:'))
n=0
if x==y:
    print('哇,猜中啦')
else:
    while n<2:
        if x>y:
            print('猜早啦,你的lucky day还没到呢')
            n=n+1
            y = eval(input('再猜:'))
        elif x<y:
            print('猜晚啦,你的lucky day已经过啦')
            n=n+1
            y = eval(input('再猜:'))
    print('次数用光啦')
    print('偷偷告诉你,5月你的lucky day 是{}号。 good luck!'.format(y))

n = eval(input("输入十进制整数x和要转换的进制n(2或8或16):"))
a=[]
zuo = num//n
a.append(num%n)
while zuo :
    a.append(zuo%n)   
    zuo = zuo//n
    print(f"{num}的{n}进制是:", end = '')
    for i in a[::-1]:
        if 10<=i<=16:
            print(chr(55+i), end = '')
        else :
                print(i, end = '')
print()
new = [str(chr(55+i)) if i not in range(10) else str(i) for i in a[::-1]]
print(f"{num}的{n}进制是:{''.join(new)}")

 

标签:语句,random,04,list1,编程,数据类型,list2,2022,print
来源: https://www.cnblogs.com/qimizhuoga/p/16194278.html