实验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,100],能

二 [1,4],不包括5

(1,4),不包括

三 一定,s1是rang(5)

四 一定,len(s2)<5不满足停止运行

task2

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

 

 

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']]
list1 = []
list2 = []
for i in temperatures:
    list1.append(i[2])
    list2.append(i[3])
a = 0
b = 0
for i in range (7):
    a = int(list1[i]) + a
    b = int(list2[i]) + b
a = int(a/7)
b = int(b/7)
print('未来一周最低气温:',min(list1),'')
print('未来一周最高气温:',max(list2),'')
print('未来一周平均气温区间:',a,''+'~',b,'')

 

 

task4

import random
num = random.randint(1,31)
print("猜猜2022年5月哪一天会是你的lucky day:-)")

for guesstake in range (1,4):
    print("你有三次机会,猜吧(1~31):")
    guess = int(input())

    if guess < num:
        print("猜早了,你的lucky day还没到呢")
    elif guess > num:
        print("猜晚了,你的lucky day已经过啦")
    else:
        break

if guess == num:
    print("哇,猜中了:-)")
else:
    print("偷偷告诉你,5月你的lucky day是2号. good luck:-)")

 

 

task5

for i in range(3):
    num, n = eval(input("输入十进制整数x和要转换的进制n(2或8或16):"))
    if n == 2:
        result = ''
        a = num
        while num != 0:
            ret = int(num % 2)
            num = int(num / 2)
            result = str(ret) + result
        print(a, '的二进制数为:', end='');print(result)
    elif n == 8:
        result = ''
        a = num
        while num != 0:
            ret = int(num % 8)
            num = int(num / 8)
            result = str(ret) + result
        print(a, '的八进制数为:', end='');print(result)
    elif n == 16:
        result = ''
        a = num
        while num != 0:
            ret = int(num % 16)
            num = int(num / 16)
            if ret >= 10:
                oout = ord('a') + ret - 10
                out = chr(oout)
                result = out + result
            else:
                result = str(ret) + result
        print(a, '的十六进制数为:', end='');print(result)
    else:
        print("你输错了吧?")

 

总结:通过本次实验,我知道了一定要随笔和pycharm同步完成,所有的代码写完并截图,然后发现代码没有复制保存真的太蠢了,照着自己的截图又重新敲了一遍,实在是太蠢了!

posted on 2022-04-27 12:37  写作业的xzy  阅读(22)  评论(4编辑  收藏  举报