task1.py
import random print('用列表存儲(chǔ)隨機(jī)整數(shù): ') ls = [random.randint(1, 100) for i in range(5)] print(ls) print(' 用集合存儲(chǔ)隨機(jī)整數(shù): ') s1 = {random.randint(1,100) for i in range(5)} print(s1) print(' 用集合存儲(chǔ)隨機(jī)整數(shù): ') s2 = set() while(len(s2)<5): s2.add(random.randint(1,100)) print(s2)
1. 范圍是1~100 可以取到100
2.范圍是0~4 不包括5
?? 1~4 不包括5
3.不一定
4.不一定
?task2
?
datas = {'2049777001': ['籃球', '羽毛球', '美食', '漫畫(huà)'], '2049777002': ['音樂(lè)', '旅行'], '2049777003': ['馬拉松', '健身', '游戲'], '2049777004': [], '2049777005': ['足球', '閱讀'], '2049777006': ['發(fā)呆', '閑逛'], '2049777007': [], '2049777008': ['書(shū)法', '電影'], '2049777009': ['音樂(lè)', '閱讀', '電影', '漫畫(huà)'], '2049777010': ['數(shù)學(xué)', '推理', '音樂(lè)', '旅行']} list1 = list(datas.values()) list2 = [] for i in range(len(list1)): for j in range(len(list1[i])): list2.append(list1[i][j]) dict_ = {} for n in list2: dict_[n]=list2.count(n) d = list(dict_.items()) e = sorted(d, key=lambda x: -x[1]) for o, k in e: print(f'{o}:{k}')
?
?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']] t1 = [] t2 = [] for ifo in temperatures: t1.append(ifo[2]) t2.append(ifo[3]) t_min = min(t1) t_max = max(t2) x = 0 y = 0 for i in t1: x += int(i) for i in t2: y += int(i) print(f'未來(lái)一周最低氣溫: {t_min}℃') print(f'未來(lái)一周最高氣溫: {t_max}℃') print(f'未來(lái)一周平均氣溫區(qū)間:{int(x/len(t1))}℃~{int(y/len(t2))}℃')
?
task4
import random
print('猜猜2022年5月哪一天會(huì)是你的lucky day:)')
lucky_day = random.randrange(1, 32)
guess = eval(input('你有三次機(jī)會(huì),猜吧(1~31):'))
i = 1
while True:
if guess > lucky_day:
print('猜晚啦,你的lucky day已經(jīng)過(guò)啦')
guess = eval(input('再猜(1~31):'))
i += 1
elif guess < lucky_day:
print('猜早啦,你的lucky day還沒(méi)到呢')
guess = eval(input('再猜(1~31):'))
i += 1
elif guess == lucky_day:
print('哇,猜中了')
break
if i == 3:
if guess > lucky_day or guess < lucky_day:
print('次數(shù)用光啦.')
print(f'偷偷告訴你,5月你的lucky day是{lucky_day}號(hào).good luck')
break
elif guess == lucky_day:
print('哇,猜中了')
break
?
?
?task5
x, n = eval(input("輸入十進(jìn)制整數(shù)x和要轉(zhuǎn)換的進(jìn)制(2或8或16):")) y = x z = [] d = ["A", "B", "C", "D", "E", "F"] while x != 0: p = x % n if n == 16: if p > 9: b = p % 10 p = y[b] z.append(p) x = x // n z.reverse() t = "".join("%s" % i for i in z) print('{}的{}進(jìn)制是:{}'.format(y, n, t))
?
本文摘自 :https://www.cnblogs.com/