SAKURA-QiQi

导航

实验三

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.是的

实验1总结:使用range函数可知range(m,n)是一个左闭右开的范围

 

task2

"""
Created on Sun Apr 24 10:25:04 2022

@author: ASUS
"""

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

v = {}
for i in datas.values():
    for j in i:
        v[j] = v.get(j, 0) + 1

x = list(v.keys())
y = list(v.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

@author: ASUS
"""

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

 

 task4

@author: ASUS
"""

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

@author: ASUS
"""

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 on 2022-04-24 10:43  QiQi-sakura  阅读(14)  评论(2编辑  收藏  举报