python加权平均绩点计算
时间: 2024-10-14 19:03:14 浏览: 54
在Python中,计算加权平均绩点(Weighted Grade Point Average,简称W-GPA)通常涉及到两个关键步骤:首先,你需要有每个课程的成绩(通常是百分制分数)及其对应的绩点值;其次,需要知道每个课程的学分数。如果你有一个字典,其中键是课程名,值是元组包含成绩和学分,你可以编写一个简单的函数来计算加权平均。
```python
def calculate_weighted_gpa(grade_dict, credit_dict):
total_grade_points = 0
total_credits = 0
for course, (grade, credit) in grade_dict.items():
if grade is not None and credit is not None:
gpa_score = grade * credit / 100 # 计算单门课绩点
total_grade_points += gpa_score
total_credits += credit
weighted_gpa = total_grade_points / total_credits if total_credits > 0 else 0
return weighted_gpa
# 示例数据
grades = {'数学': (95, 4), '英语': (88, 3), '物理': (90, 3)}
credits = {'数学': 4, '英语': 3, '物理': 3}
weighted_avg = calculate_weighted_gpa(grades, credits)
print(f"加权平均绩点: {weighted_avg}")
阅读全文