实验3 控制语句与组合数据类型应用编程

#task1
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.范围【1,100】,能取到100
2.范围【0,5),不能取到5
范围【1,5),不能取到5
3.一定是
4.一定是
###
#task2
datas={'2049777001':['篮球','羽毛球','美食','漫画'],
       '2049777002':['音乐','旅行'],
       '2049777003':['马拉松','健身','游戏'],
       '2049777004':[],
       '2049777005':['足球','阅读'],
       '2049777006':['发呆','闲逛'],
       '2049777007':[],
       '2049777008':['书法','电影'],
       '2049777009':['音乐','阅读','电影','漫画'],
       '2049777010':['数学','推理','音乐','旅行']
       }
ls=list(datas.values())
l1=[]
l2=[]
for i in datas.values():
       for j in i:
              l1.append(j)
l3=set(l1)
for k in l3:
       x=l1.count(k)
       l2.append(x)
l4=dict(zip(l3,l2))
m=[(v,k) for k,v in l4.items()]
n=sorted(m,reverse=True)
for x in n:
       print(f'{x[1]}:{x[0]}')

#task3
temperatures=[['2022-4-18','星期一','10','22'],
              ['2022-4-19','星期二','10','22'],
              ['2022-4-20','星期三','12','25'],
              ['2022-4-21','星期四','13','28'],
              ['2022-4-22','星期五','16','31'],
              ['2022-4-23','星期六','19','31'],
              ['2022-4-24','星期日','19','27']]
a=[i[2] for i in temperatures]
b=[i[3] for i in temperatures]
max=max(b)
min=min(a)
s1=s2=0
for j in a:
    s1+=int(j)
for k in b:
    s2+=int(k)
v1=s1//7
v2=s2//7
print('未来一周最低气温:',min)
print('未来一周最高气温:',max)
print('未来一周平均气温区间:',v1,'~',v2)

#task4
import random
print('猜猜2022年5月哪一天会是你的Lucky Day:-}')
right_number=random.randint(1,31)
s=eval(input('你有三次机会,猜吧(1~31):'))
t=2
while t!=0:
    if s==right_number:
        print('哇,猜中了:-)')
        break
    else:
        if s>right_number:
            print('猜晚啦,你的Lucky Day已经过啦')
        elif s<right_number:
            print('猜早啦,你的Lucky Day还没到呢')
            t-=1
        s = eval(input('再猜(1~31):'))
        continue
if t==0:
    print('次数用光啦。')
    print('偷偷告诉你,5月你的Lucky Day是',right_number,'号,good luck:-)')
#我真的截不到猜对的图……#
#task5
x,n=eval(input('输入十进制整数x和要转换的进制n(2或8或16):'))
a=x
i=[]
t={10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
if n==2:
    while x>=1:
        b=x%n
        i.append(int(b))
        x=(x-b)/n
elif n==8:
    while x>=1:
        b=x%n
        i.append(int(b))
        x=(x-b)/n
elif n==16:
    while x>=1:
        b=x%n
        if b>=10:
            i.append(int(t[b]))
        else:
            i.append(int(b))
        x=(x-b)/n
i.reverse()
print(f'{a}的{n}进制是:',end='')
for t in i:
    print(t,end='')

 

 

 

 

posted @ 2022-04-26 23:24  孙艺欣  阅读(21)  评论(3编辑  收藏  举报