酷代码 AI
菜单
服务商

Python实现学生成绩管理:输入格式、数据类型与成绩赋值需谨慎!

```python class Score: def __init__(self, usual_score, final_score): # 初始化平时成绩和期末成绩 self.usual_score = int(usual_score) self.final_score = int(final_score) def calculate_total_score(self): # 计算总成绩,平时成绩占40%,期末成绩占60%,取整数部分 return int(self.usual_score * 0.4 + self.final_score * 0.6) class Student: def __init__(self, student_id, name): # 初始化学号、姓名 self.student_id = student_id self.name = name # 初始化语文、数学、物理成绩为None self.chinese_score = None self.math_score = None self.physics_score = None def calculate_total_score(self): # 计算学生的总分 return self.chinese_score.calculate_total_score() + self.math_score.calculate_total_score() + self.physics_score.calculate_total_score() def calculate_average_score(self): # 计算学生的平均分,保留两位小数 return round(self.calculate_total_score() / 3, 2) students = {} for _ in range(9): info = input().split() student_id, name, course, usual_score, final_score = info if student_id not in students: # 如果学生不存在,创建新的学生对象 students[student_id] = Student(student_id, name) student = students[student_id] score = Score(usual_score, final_score) if course == "语文": student.chinese_score = score elif course == "数学": student.math_score = score elif course == "物理": student.physics_score = score for student in students.values(): total_score = student.calculate_total_score() average_score = student.calculate_average_score() chinese_total = student.chinese_score.calculate_total_score() math_total = student.math_score.calculate_total_score() physics_total = student.physics_score.calculate_total_score() print(f"{student.student_id} {student.name} {total_score} {average_score:.2f} {chinese_total:.2f} {math_total:.2f} {physics_total:.2f}") ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 输入数据的格式必须严格按照要求,若输入数据格式错误,可能导致程序无法正确解析数据。 2. 在计算总成绩和平均分时,要注意数据类型的转换,避免出现计算错误。 3. 确保每个学生的三门课程成绩都被正确赋值,否则在计算总分和平均分的时候会出现异常。 ######[AI写代码神器 | 629点数解答 | 2025-10-30 16:52:22]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]