import pandas as pd import random # 读取学生名单 Excel 文件 def read_student_list(file_path): df = pd.read_excel(file_path) df['积分'] = 0 # 初始化每个学生的积分为0 return df # 学生名单保存在 students.xlsx 文件中 students = read_student_list('students.xlsx') print(students) # 点名规则:按积分计算选中的概率 def weighted_random_choice(df): total_weight = sum([1 / (1 + score) for score in df['积分']]) r = random.uniform(0, total_weight) upto = 0 for i, row in df.iterrows(): weight = 1 / (1 + row['积分']) if upto + weight >= r: return i # 返回被点名的学生索引 upto += weight # 更新积分 def update_score(df, index, answer_correctness): df.loc[index, 'score'] += 1 # 点到一次+1 # 根据回答情况调整积分 if answer_correctness == 'wrong': df.loc[index, 'score'] -= 1 elif answer_correctness == 'partial': df.loc[index, 'score'] += 1 elif answer_correctness == 'correct': df.loc[index, 'score'] += 3 # 示例:更新一个被选中学生的积分 selected_index = weighted_random_choice(students) update_score(students, selected_index, 'correct') # 保存更新后的名单 def save_to_excel(df): df.to_excel("updated_students.xlsx", index=False) # 示例 save_to_excel(students) # 主循环,随机点名并更新积分 def classroom_roll_call(file_path, num_sessions): for _ in range(num_sessions): selected_index = weighted_random_choice(students) # 模拟回答情况 answer = random.choice(['wrong', 'partial', 'correct']) update_score(students, selected_index, answer) # 示例:进行10次点名 classroom_roll_call('students.xlsx', 10)