对学生阳光分班的一种python实现,界面由pysimplegui实现

目前各地学校都被要求阳光分班,也就是公平公正的为学生安排班级,但是从实现上,好多是教务员手工复制、粘贴完成的,本文作者系教师,较为了解一线教师的辛苦,这里呈现一种python语言的实现,前几日发给一位工作在小学的好友使用。

代码中实现了每个班人数最多与最少只能差1,保证每个班人数基本均衡。

缺点:没有考虑性别的均衡。

底部提供下载

import random
import PySimpleGUI as sg
import xlwt,os
import xlrd
#对每一位学生分班处理
def handlestudentone(rowlist,classnum,classmax_num,class_studentlists,yushu_mod):
    tempno = random.randint(0, classnum - 1)
    print('该{}班已有学生数:'.format(tempno + 1), len(class_studentlists[tempno]))
    n=0  #达到最大人数的班级
    for x in range(len(class_studentlists)):
        if len(class_studentlists[x])==classmax_num:
            n=n+1
    if n==yushu_mod and len(class_studentlists[tempno]) == classmax_num-1:
        return handlestudentone(rowlist, classnum, classmax_num, class_studentlists,yushu_mod)


    if len(class_studentlists[tempno]) < classmax_num  :
        class_studentlists[tempno].append(rowlist)
        return tempno+1

    else:
        return handlestudentone(rowlist, classnum, classmax_num, class_studentlists,yushu_mod)

#pysimplegui界面
sg.ChangeLookAndFeel('LightGreen')

cancel_btn = sg.Button('开始分班', size=(10, 2), font=("微软雅黑", 12))
layout = [

    #[sg.Checkbox('3位数减法,退位,只能与竖式同时选择', size=(35, 2),text_color=('blue'), font=("微软雅黑", 12))],
   # [sg.Slider(range=(1,5), orientation='h',size=(35, 10),  font=("微软雅黑", 12))],
    #[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
    #[sg.Spin([1,2,3,4,5], size=(35, 10),  font=("微软雅黑", 12))],
[sg.Text('设置班级数:',auto_size_text=True,size=(15, 1), font=("微软雅黑", 12))],
[sg.Slider(range=(1,22), orientation='h',size=(35, 15),  font=("微软雅黑", 12))],

[sg.Text("导入学生数据:",auto_size_text=True,size=(15, 1), font=("微软雅黑", 12))],   [sg.Input(), sg.FileBrowse("打开数据",file_types=(("Excel2003文件", "*.xls"),("Excel2007之后文件", "*.xlsx")))],
    [ cancel_btn],[sg.StatusBar('作者: \n邮箱:45251289@qq.com\n版本日期:2020-8-17',size=(400,10), font=("微软雅黑", 12))]

]

window = sg.Window('学生随机分班器', default_element_size=(40, 2), size=(400, 285)).Layout(layout)

#处理界面事件
while True:
    event, values = window.read()
    if event in (None, '导入学生数据'):
        # User closed the Window or hit the Cancel button
        break
    elif event in (None, '开始分班'):
        #sg.Popup(event, values)

        #print(values['打开数据'])
        #print(values[0])
        data = xlrd.open_workbook(values['打开数据'])
        table = data.sheets()[0]
        nrows = table.nrows  # 获取该sheet中的有效行数
        ncols= table.ncols# 获取该sheet中的有效列数
        print(nrows)
        wb = xlwt.Workbook()
        ws = wb.add_sheet('分班结果', cell_overwrite_ok=True)  # 增加sheet
        for firstrow in range(0,ncols):
            ws.write(0,firstrow,table.row(0)[firstrow].value)
        ws.write(0, ncols,'分班结果')
        totalstudentnum=nrows-1
        classnum=int(values[0])
        classmin_num=totalstudentnum//classnum
        classmax_num=classmin_num+1
        if totalstudentnum % classnum==0:
            classmax_num=classmin_num

        class_studentlists = [[] for j in range(classnum)]

        #print(len(class_studentlists))
        style = xlwt.XFStyle()
        style.num_format_str = 'YYYY-MM-DD'



        for i in range(1,nrows):
            
            class_id=handlestudentone(table.row(i), classnum, classmax_num, class_studentlists,totalstudentnum % classnum)
            for firstrow in range(0, ncols):
                if table.row(i)[firstrow].ctype==3:
                    ws.write(i, firstrow, table.row(i)[firstrow].value,style)
                ws.write(i, firstrow, table.row(i)[firstrow].value)
                print("数据类型:", table.row(i)[firstrow].ctype)
            ws.write(i, firstrow+1, "{}班".format(class_id))



        for l in range(len(class_studentlists)):


            print("{}班人数:".format(l+1),len(class_studentlists[l]))

        wb.save('分班结果.xls')
        os.startfile("分班结果.xls")


        break

window.close()

经过pyinstaller打包成exe:下载链接

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以通过编写程序来实现学生分班。下面是一个示例代码: ```python # 学生信息 students = [ {'name': '小明', 'gender': '男', 'score': 80}, {'name': '小红', 'gender': '女', 'score': 90}, {'name': '小刚', 'gender': '男', 'score': 70}, {'name': '小美', 'gender': '女', 'score': 85}, {'name': '小强', 'gender': '男', 'score': 75}, {'name': '小花', 'gender': '女', 'score': 95}, ] # 定义班级 class1 = [] class2 = [] # 分班规则 for student in students: if student['gender'] == '男': if len(class1) < len(class2): class1.append(student) else: class2.append(student) else: if student['score'] > 85: class1.append(student) else: class2.append(student) # 打印分班结果 print("班级1:") for student in class1: print(student['name']) print("班级2:") for student in class2: print(student['name']) ``` 通过这段代码,首先定义了一个存储学生信息的列表,每个学生包括姓名、性别和分数。然后定义了两个班级的列表,用来存储分班结果。 接着,根据分班规则对学生进行循环遍历。对于男生,根据班级人数的大小判断分配到班级1还是班级2;对于女生,根据分数高低判断分配到班级1还是班级2。 最后,通过print语句打印出分班结果,分别输出班级1和班级2的学生姓名。 注意:这只是一个简单的示例代码,实际分班可能需要考虑更多因素,如班级人数限制、分数区间等。具体实现方法可以根据实际需求进行适当修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值