酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

某中学对学生的附加科目进行能力测试,并按以下标准统计学科等级水平。 (1)生物和科学两门课都达到60分,并且总分达到300分为及格; (2)每门课达到85分,并且总分达到425分为优秀; (3)总分不到250分或有任意一门课不到60分,为不及格。 请从score.txt文件中读取学生成绩数据,判定等级并写入level.txt文件中。 1)score.txt 文件中数据组织形式:文件中的第一行是数据标签,数据从第二行开始,空格间 隔),例 学号 姓名 语文 数学 英语 生物 科学 001 赵晨 85 78 99 70 60 …… 2)level.txt 文件中数据组织形式:文件中的第一行是数据标签,数据从第二行开始,空格间 隔),例 学号 姓名 总成绩 达标 001 …… 赵晨 392 及格

首先,我们需要从score.txt文件中读取学生成绩数据,然后根据给定的标准判断学生的等级,并将结果写入level.txt文件中。 解析: 1. 从score.txt文件中读取学生成绩数据 2. 根据给定的标准判断学生的等级 3. 将结果写入level.txt文件中 代码如下: ```python def read_score(file_path): with open(file_path, 'r') as f: lines = f.readlines() data = [] for line in lines[1:]: item = line.strip().split() data.append((item[0], item[1], int(item[2]), int(item[3]), int(item[4]), int(item[5]), int(item[6]))) return data def judge_level(data): result = [] for item in data: total_score = sum(item[2:]) if item[4] >= 60 and item[5] >= 60 and total_score >= 300: level = '及格' elif item[2] >= 85 and item[3] >= 85 and item[4] >= 85 and item[5] >= 85 and total_score >= 425: level = '优秀' else: level = '不及格' result.append((item[0], item[1], total_score, level)) return result def write_level(file_path, data): with open(file_path, 'w') as f: f.write('学号 姓名 总成绩 达标 ') for item in data: f.write(f'{item[0]} {item[1]} {item[2]} {item[3]} ') if __name__ == '__main__': score_file = 'score.txt' level_file = 'level.txt' score_data = read_score(score_file) level_data = judge_level(score_data) write_level(level_file, level_data) ``` 这段代码首先定义了三个函数:`read_score`用于从score.txt文件中读取学生成绩数据;`judge_level`用于根据给定的标准判断学生的等级;`write_level`用于将结果写入level.txt文件中。通过调用这三个函数完成任务。 [2024-06-22 23:14:14 | AI写代码神器 | 573点数解答]

相关提问