A39523. 随着新冠肺炎的良好控制,学校也陆陆续续开始了复学。为了师生们的安全,统计了一组学生的信息如下: 你需要编写程序,使用 python 中合适的方式存储 4 个学生的信息,并且完成以下要求: (存储方式包括但不限于:列表,字典,类与对象等……) 1.补全函数 age_avg(),调用函数打印出学生的平均年龄,求出的平均年龄为整数。 2.补全函数 find_stu(),调用函数传入参数城市名字,打印出…
填空题
困难
知识点
题目描述
随着新冠肺炎的良好控制,学校也陆陆续续开始了复学。为了师生们的安全,统计了一组学生的信息如下:

你需要编写程序,使用 python 中合适的方式存储 4 个学生的信息,并且完成以下要求:
(存储方式包括但不限于:列表,字典,类与对象等……)
1.补全函数 age_avg(),调用函数打印出学生的平均年龄,求出的平均年龄为整数。
2.补全函数 find_stu(),调用函数传入参数城市名字,打印出停留过该城市的学生的名字,年龄和体温。如果没有查找到信息,打印无。
3.补全函数 sort_stu(),调用函数对体温进行从高到低的排序,并且打印出学生名字以及对应的体温。请删除 pass,编写代码,补全三个函数。注意不要修改预设函数的名字。
参考答案
#创建学生类
class Student:
def __init__(self, name, age, city, temp):
self.name=name
self.age=age
self.city=city
self.temp=temp
# 初始化学生列表,创建4个学生对象录入信息
stu_list = []
stu_list.append(Student("小雅",13,"武汉",38.2))
stu_list.append(Student("白白",9,"上海" ,36.5))
stu_list.append(Student("祥龙",10,"杭州",35.7))
stu_list.append(Student("晓秋",15,"北京",36.2))
#计算平均年龄
def age_avg():
count =0
for stu in stu_list:
count += stu.age
print(count // len(stu_list))
#根据城市寻找学生
def find_stu():
city = input()
for stu in stu_list:
if city == stu.city:
print(stu.name, stu.age, stu.temp)
return
print('无')
# 根据学生体温,对学生进行从高到低的排序
def sort_stu():
for i in range(len(stu_list) - 1):
for j in range(len(stu_list)-i-1):
if stu_list[j].temp < stu_list[j + 1].temp:
stu_list[j],stu_list[j+ 1] = stu_list[j+1], stu_list[j]
for stu in stu_ist:
print(stu.name,stu.temp)
age_avg()
find_stu()
sort_stu()
上一题
下一题