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

task1.py

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.

2.list(range(5))和list(range(1,5))生成的有序数列范围都不包含5.

3.len(s1)一定是5.

4len(s2)一定是5.

task2.py

 

datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
         '2049777002': ['音乐', '旅行'],
         '2049777003': ['马拉松', '健身','游戏'],
         '2049777004': [],
         '2049777005': ['足球', '阅读'],
         '2049777006': ['发呆', '闲逛'],
         '2049777007': [],
         '2049777008': ['书法', '电影'],
         '2049777009': ['音乐', '阅读', '电影', '漫画'],
         '2049777010': ['数学', '推理', '音乐', '旅行'] }
ls=list(datas.values())
l1=[]
dict={}
for each  in ls:
    a=each
    for b in a:
        l1.append(b)
        for i in l1:
            c=l1.count(i)
            dict[i]=c
l2=[(x[1],x[0]) for x in list(dict.items())]
l2.sort(reverse=bool(1))
for key,value in l2:
    print(f'{value}:{key}')
        

 task3.py

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']]
min=temperatures[0][2]
for i in range(len(temperatures)):
    if(min>temperatures[i][2]):
        min=temperatures[i][2]
print(f'未来一周最低气温:{min}℃')
max=temperatures[0][3]
for i in range(len(temperatures)):
    if(max<temperatures[i][3]):
        max=temperatures[i][3]
print(f'未来一周最高气温:{max}℃')
a=len(temperatures)
k=[]
l=[]
for i in temperatures:
    k.append(int(i[2]))
    l.append(int(i[3]))
sum1=0
sum2=0
for s in k:
    sum1+=s
for b in l:
    sum2+=b
    m=int(sum1/a)
    n=int(sum2/a)
print(f'未来一周平均气温区间{m}℃~{n}℃')

task4.py

import random

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

 task5.py

x,n=eval(input('输入十进制整数x和要转换的进制n(2或8或16):'))
k=x
a = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
b=[]
m=0
while m==0:
    s=x//n
    y=x%n
    b=b+[y]
    if s==0:
        break
    x=s
b.reverse()
if n==2:
    print(k,'转换为二进制后是:',end='')
    for i in b:
         print(a[i],end='')
if n==8:
    print(k,'转换为八进制后是:',end='')
    for i in b:
         print(a[i],end='')
if n==16:
    print(k,'转换为十六进制后是:',end='')
    for i in b:
         print(a[i],end='')

 

posted @ 2022-04-25 13:29  星标g  阅读(9)  评论(3编辑  收藏  举报