实验3

实验3

task3_1

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)

Q1:[1,100],能

Q2:[0,4],否; [1,4],否

Q3:是

Q4:是

task3_2

 

datas={'2049777001':['篮球','羽毛球','美食','漫画'],
       '2049777002':['音乐','旅行'],
       '2049777003':['马拉松','健身','游戏'],
       '2049777004':[],
       '2049777005':['足球','阅读'],
       '2049777006':['发呆','闲逛'],
       '2049777007':[],
       '2049777008':['书法','电影'],
       '2049777009':['音乐','阅读','电影','漫画'],
       '2049777010':['数学','推理','音乐','旅行']
       }
habits={}
for value in datas.values():
    for habit in value:
        if habits.get(habit):
            habits[habit]+=1
        else:
            habits[habit]=1
d1=[(value,key)for key,value in habits.items()]
for key,value in sorted(d1,reverse=True):
    print(f'{value}:{key}')

task3_3

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']]
i=0
a=0
b=0
m=int(temperatures[0][2])
n=int(temperatures[0][2])
while i<7:
    x=int(temperatures[i][2])
    y=int(temperatures[i][3])
    a+=x
    b+=y
    if x<m:
        m=x
    if y>n:
        n=y
    i+=1
c=int(a/7)
d=int(b/7)
print('未来一周最低气温:',m,'')
print('未来一周最高气温:',n,'')
print('未来一周平均气温区间:',c,'','~',d,'')

task3_4

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

 task3_5

x=int(input('输入十进制整数:'))
n=int(input('要转换的进制:'))
a=[]
if n==2 or n==8:
    while x//n!=0:
        a.insert(0,x%n)
        x=x//n
    else:
        a.insert(0,x%n)
elif n==16:
    b=int(x%n)
    while x//n!=0:
        if b==10:
            b='A'
        elif b==11:                                                                                         
            b='B'
        elif b==12:
            b='C'
        elif b==13:
            b='D'
        elif b==14:
            b='E'
        elif b==15:
            b='F'   
        elif 0<=b<=9:
            b=str(x%n)
        a.insert(0,b)
        x=x//n
    else:
        a.insert(0,x%n)
for i in a:
    print(i,end='')

 

 

posted @ 2022-04-24 23:30  周易潇  阅读(6)  评论(1编辑  收藏  举报