实验3

2022/4/20 23:13:35

本文主要是介绍实验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)

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

hobbies={}
values=datas.values()
for value in values:
    for hobby in value:
        if hobby in hobbies.keys():
            hobbies[hobby]=hobbies[hobby]+1
        else:
            hobbies[hobby]=1

hobbies=sorted(hobbies.items(),key = lambda x:x[1],reverse = True)
for hobby,num in hobbies:
    print(hobby,':',num)

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']]

low=[]
high=[]

for row in temperatures:
    low.append(int(row[2]))
    high.append(int(row[3]))
low_temp=100
high_temp=0
sum_low=0
sum_high=0
for i in range(len(low)):
    if low[i]<low_temp:
        low_temp=low[i]
    if high[i]>high_temp:
        high_temp=high[i]
    sum_low+=low[i]
    sum_high+=high[i]

print('未来一周最低气温:',low_temp,'℃')
print('未来一周最低气温:',high_temp,'℃')
print('未来一周平均气温区间:',sum_low//len(low),'℃~',sum_high//len(high),'℃')

import random
random_num=random.randint(1,32)
print("猜猜2022年5月哪一天会是你的lucky day:~")
print('你有三次机会,猜吧(1-31):')

cishu=0
while cishu<=3:
    num=int(input())
    if num<random_num:
        print('猜早啦,你的lucky day还没到呢')
    elif num>random_num:
        print('猜晚啦,你的lucky day已经过啦')
    else:
        print('哇,猜中了')
        break
    if cishu < 2:
        print('再猜(1-31):')
    else:
        print('次数用光啦')
        print('偷偷告诉你,5月你的lucky day是',random_num,'号,good luck')
    cishu+=1

print('输入十进制整数x和要转换的进制n(2或8或16):')
x=int(input())
n=int(input())

result=[]
while x!=0:
    result.append(x%n)
    x=x//n
if n == 16:
    duiying={10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
    for i in range(len(result)):
        if result[i]>=10:
            result[i]=duiying[result[i]]

result=list(reversed(result))
for num in result:
    print(num,end='')

 



这篇关于实验3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程