第三次作业初版

import openpyxl
#1.文件写入
def write_data(dic):#将数据写入excel,并保存
    book = openpyxl.load_workbook("学生成绩表.xlsx")
    ws = book["学生成绩表"]
    ws.append(score_sum)
    book.save("学生成绩表.xlsx")

# 2.录入成绩
def input_score():
    dic = dict()
    dic['student_number'] = input("请输入学号:")
    dic['name'] = input("请输入学生姓名:")
    dic['math'] = '%.2f' % float(input("请输入数学成绩:"))
    dic['english'] = '%.2f' % float(input("请输入英语成绩:"))
    dic['info'] = '%.2f' % float(input("请输入信息成绩:"))
    dic['ave_score'] = '%.2f' % float((dic['math'] + dic['english'] + dic['info'])/3)
    return dic

#3.查看学生成绩
def examine_score():
    book = openpyxl.load_workbook("学生成绩表.xlsx")
    ws = book["学生成绩表"]
    examine_all_score = list()
    for row in range(1, ws.max_row + 1):
        sigle_score = list()
        for column in range(1, ws.max_column + 1):
            sigle_score.append(ws.cell(row,column).value)
            print(ws.cell(row,column).value,end=" ")
        examine_all_score.append(sigle_score)
        print("")
    return examine_all_score

#4.查看排序后的学生成绩
def takeSecond(elem):
    return elem[5]

def examine_sort_score():
    book = openpyxl.load_workbook("学生成绩表.xlsx")
    ws = book["学生成绩表"]
    all_score = list()
    for row in range(1, ws.max_row + 1):
        sigle_score = list()
        for column in range(1, ws.max_column + 1):
            sigle_score.append(ws.cell(row,column).value)
        all_score.append(sigle_score)
    st_sc = all_score[1:]
    st_sc.sort(key=takeSecond, reverse=True)
    st_sc.insert(0, all_score[0])
    for i in range(1, len(st_sc) + 1):
        for j in range(1, len(st_sc[i-1]) + 1):
            ws.cell(i,j,st_sc[i-1][j-i])
        if i == 1:
            ws.cell(i, (len(st_sc[i - 1]) + 1), "名次")
        else:
            ws.cell(i, (len(st_sc[i - 1]) + 1), i - 1)
    book.save("学生成绩表.xlsx")
    examine_score()
    return all_score
    
#5.成绩统计
#6.成绩修改和删除
def modify_score(student_info, action):
    book = openpyxl.load_workbook("学生成绩表.xlsx")
    ws = book["学生成绩表"]
    list_all_score = examine_score()
    if action == 0:
        for i in list_all_score:
            if student_info == i:
                row_number = list_all_score.index(i) + 1
                ws.delete_rows(row_number)
                book.save("学生成绩表.xlsx")
            else:
                print("你要删除的学生信息不存在!")
    elif action == 1:
        for i in list_all_score:
            if student_info == i:
                row_number = list_all_score.index(i) + 1
                for j in range(1, len(i) + 1):
                    ws.cell(row_number, j, i[j - 1])
                book.save("学生成绩表.xlsx")
            else:
                print("你要修改的学生信息不存在!")

def main():
    menu = '''
************欢迎进入学生成绩管理系统*****************
                    菜单
输入成绩-------------------------------------------------1
查看成绩及成绩排序---------------------------------------2
查询学生成绩---------------------------------------------3
成绩统计-------------------------------------------------4
修改成绩-------------------------------------------------5
添加成绩-------------------------------------------------6
删除成绩-------------------------------------------------7
退出系统-------------------------------------------------8
*******************************************************
# '''
    print(menu)
    choice = int(input("请输入你的选择:"))
    if choice == 1:
        student_info = input_score()
        write_data(student_info)
        print("录入成功!")
    elif choice == 2:
        examine_score()
        print("是否需要对成绩做排序:")
        print("1.是")
        print("2.不是")
        examine = int(input("请选择:"))
        if examine == 1:
            examine_sort_score()
        elif examine == 2:
            examine_score()
    # elif choice == 3:
        
# 

if __name__ == '__main__':
    main()
# 

 

posted on 2021-05-15 18:08  torotoise512  阅读(45)  评论(0)    收藏  举报