- 创建 .idea 目录和相关配置文件,设置项目结构 - 添加多个课堂成果示例代码,涵盖不同主题和功能 - 创建和配置 .gitignore 文件,忽略特定文件和目录
48 lines
1.5 KiB
Python
48 lines
1.5 KiB
Python
# 运行程序前, 请务必前往本课包【课程说明】处,下载课程素材,完成配置
|
|
|
|
import csv
|
|
|
|
# 获取各字典中的总分
|
|
def ave(s): # s是列表中的每一项
|
|
return s['总分']
|
|
|
|
# 读取CSV文件并计算总分进行排序
|
|
def process(filename):
|
|
students = []
|
|
with open(filename, 'r') as csvfile:
|
|
reader = csv.DictReader(csvfile)
|
|
for row in reader:
|
|
# 提取学生姓名和成绩,并转换成整数
|
|
name = row['姓名']
|
|
landmark_score = int(row['地标挑战赛成绩'])
|
|
history_score = int(row['历史挑战赛成绩'])
|
|
life_score = int(row['生活常识挑战赛成绩'])
|
|
|
|
# 计算总分
|
|
total_score = landmark_score + history_score + life_score
|
|
|
|
# 创建一个字典存储学生信息和总分
|
|
stu_info = {
|
|
'姓名': name,
|
|
'总分': total_score
|
|
}
|
|
|
|
# 将字典添加到列表中
|
|
students.append(stu_info)
|
|
|
|
# 根据总分对学生进行排序(降序)
|
|
students.sort(key=ave, reverse=True)
|
|
|
|
return students
|
|
|
|
# 打印排名结果
|
|
def print_rank(students):
|
|
print("排名\t姓名\t总分")
|
|
for i, student in enumerate(students, start=1):
|
|
print(f"{i}\t{student['姓名']}\t{student['总分']}")
|
|
|
|
# 主程序
|
|
students = process('scores.csv') # 指定CSV文件
|
|
print_rank(students)
|
|
print(f"代表班级参加知识竞赛的是:{students[0]['姓名']}")
|