实验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.生成的范围是1-100,包括1和100

2.[0,1,2,3,4]不包括5

[1,2,3,4],不包括5

3.是的

4.是的

task2

datas = {'2049777001': ['篮球', '羽毛球', '美食', '漫画'],
'2049777002': ['音乐', '旅行'],
'2049777003': ['马拉松', '健身', '游戏'],
'2049777004': [],
'2049777005': ['足球', '阅读'],
'2049777006': ['发呆', '闲逛'],
'2049777007': [],
'2049777008': ['书法', '电影'],
'2049777009': ['音乐', '阅读', '电影', '漫画'],
'2049777010': ['数学', '推理', '音乐', '旅行']}
s = {}
for i in datas.values():
    for j in i:
        s[j] = s.get(j, 0) + 1

x = list(s.keys())
y = list(s.values())
z = [(y[i], x[i]) for i in range(len(x))]
for j in sorted(z, reverse=True):
    print(f"{j[1]}: {j[0]}")

task3

import math
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_temp = []
max_temp = []
for i in temperatures:
    min_temp.append(i[2])
    max_temp.append(i[3])
min_temp.sort()
max_temp.sort(reverse=True)
print(f"未来一周最低温度:{min_temp[0]}℃\n未来一周最高温度:{max_temp[0]}℃")


def aver(z):
    """计算平均值"""
    sum_ = 0
    for q in z:
        sum_ += int(q)
    ave = sum_ / len(z)
    return ave


print(f"未来一周平均气温区间:{math.floor(aver(min_temp))}~{math.floor(aver(max_temp))}")

task4

from random import randint

print("猜猜2022年5月哪一天会是你的lucky day:-")
x = int(input("你有三次机会,猜吧(1~31): "))
y = randint(1, 31)
for i in range(3):
    if x == y:
        print("哇,猜中了:-)")
        break
    elif x < y:
        print("猜早啦,你的lucky day还没到呢")
    else:
        print("猜晚啦,你的lucky day已经过啦")
    if i < 2:
        x = int(input("再猜(1~31): "))
else:
    print(f"次数用完啦。\n偷偷告诉你,5月你的lucky day是{y}号。good luck:-)")

task5

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

 

posted @ 2022-04-26 23:30  桑羽  阅读(12)  评论(5编辑  收藏  举报