实验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. 范围是1~100 可以取到100

2.范围是0~4 不包括5

   1~4 不包括5

3.不一定

4.不一定

 task2

 

datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
         '2049777002': ['音乐', '旅行'],
         '2049777003': ['马拉松', '健身', '游戏'],
         '2049777004': [],
         '2049777005': ['足球', '阅读'],
         '2049777006': ['发呆', '闲逛'],
         '2049777007': [],
         '2049777008': ['书法', '电影'],
         '2049777009': ['音乐', '阅读', '电影', '漫画'],
         '2049777010': ['数学', '推理', '音乐', '旅行']}
list1 = list(datas.values())
list2 = []
for i in range(len(list1)):
    for j in range(len(list1[i])):
        list2.append(list1[i][j])
dict_ = {}
for n in list2:
    dict_[n]=list2.count(n)
d = list(dict_.items())
e = sorted(d, key=lambda x: -x[1])
for o, k in e:
    print(f'{o}:{k}')

 

 task3

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']]
t1 = []
t2 = []
for ifo in temperatures:
    t1.append(ifo[2])
    t2.append(ifo[3])
t_min = min(t1)
t_max = max(t2)
x = 0
y = 0
for i in t1:
    x += int(i)
for i in t2:
    y += int(i)
print(f'未来一周最低气温: {t_min}℃')
print(f'未来一周最高气温: {t_max}℃')
print(f'未来一周平均气温区间:{int(x/len(t1))}℃~{int(y/len(t2))}℃')

 

task4

import random
print('猜猜20225月哪一天会是你的lucky day:)')
lucky_day = random.randrange(1, 32)
guess = eval(input('你有三次机会,猜吧(1~31):'))
i = 1
while True:
if guess > lucky_day:
print('猜晚啦,你的lucky day已经过啦')
guess = eval(input('再猜(1~31):'))
i += 1
elif guess < lucky_day:
print('猜早啦,你的lucky day还没到呢')
guess = eval(input('再猜(1~31):'))
i += 1
elif guess == lucky_day:
print('哇,猜中了')
break
if i == 3:
if guess > lucky_day or guess < lucky_day:
print('次数用光啦.')
print(f'偷偷告诉你,5月你的lucky day{lucky_day}.good luck')
break
elif guess == lucky_day:
print('哇,猜中了')
break

 

 

 task5

x, n = eval(input("输入十进制整数x和要转换的进制(2或8或16):"))
y = x
z = []
d = ["A", "B", "C", "D", "E", "F"]
while x != 0:
    p = x % n
    if n == 16:
        if p > 9:
            b = p % 10
            p = y[b]
    z.append(p)
    x = x // n
z.reverse()
t = "".join("%s" % i for i in z)
print('{}的{}进制是:{}'.format(y, n, t))

 

posted @ 2022-04-25 22:02  东揚  阅读(6)  评论(3编辑  收藏  举报