# 随机生成4个学生一周的体温,并求其平均温度
import random
t=[] #创建空列表
for i in range(7):
t.append(round(random.uniform(36, 38),2))#
print(t)
avg=sum(t)/len(t)
print(avg)
C = [[] for i in range(4)] #先列后行(4行不知道多少列)
for i in range(4):
for j in range(7):
t = round(random.uniform(36, 38),2)
C[i].append(t)
print(C)
# 计算每位同学一周的平均体温
avg_t = []
t = 0
for row in C:
t=0
for col in row:
t = t + col
avg_t.append(round(t / 7,2))
print(avg_t)
输出:
[36.47, 37.0, 37.93, 37.21, 37.37, 36.49, 37.88] 37.19285714285714 [[37.05, 36.17, 37.34, 37.18, 37.96, 36.78, 36.12], [36.8, 37.51, 37.96, 37.07, 36.11, 37.86, 37.38], [36.92, 36.78, 37.59, 37.4, 36.05, 36.56, 37.33], [37.25, 37.18, 36.6, 36.44, 37.34, 37.44, 36.54]] [36.94, 37.24, 36.95, 36.97]