公司网站 钓鱼网站,网站建设实训报告的内容怎么写,苏州网站建设新手,德化住房和城乡建设网站目录 一、学生管理系统#xff56;1.0#xff08;需求#xff09;
二、代码实现 一、学生管理系统#xff56;1.0#xff08;需求#xff09; 1、添加学生信息 2、删除学生信息 3、查看学生信息 4、修改学生信息
二、代码实现
# 使用字典存储学生信息
students {}# 学…目录 一、学生管理系统1.0需求
二、代码实现 一、学生管理系统1.0需求 1、添加学生信息 2、删除学生信息 3、查看学生信息 4、修改学生信息
二、代码实现
# 使用字典存储学生信息
students {}# 学号
ID 1000# 退出学生管理系统
flag Truedef add_student():添加学生信息global studentsglobal IDstudent {}# 获得学生名字name input(请输入学生姓名:)# 获得学生成绩score input(请输入学生成绩:)# 学号ID 1# 保存学生成绩student[name] namestudent[score] score# 将学生信息保存到学生列表中students[ID] studentprint(新学生信息添加成功!)def del_student():删除学生信息global studentsdelete_id int(input(请输入要删除的学生ID:))if delete_id in students.keys():# 删除学生信息del students[delete_id]# 删除成功提示print(学号为%d的学生信息删除成功! % delete_id)else:print(删除的学生ID不存在!)def show_students():打印学生信息for key, student in students.items():print(学号:%d \t 姓名:%s \t 分数:%s % (key, student[name], student[score]))print(------------)def save_students():保存学生信息到文件stu_info []# 创建并打开文件stus_file open(students.txt, w)for key, student in students.items():stu_info.append(str(key))stu_info.append(student[name])stu_info.append(student[score])stu_data ,.join(stu_info)# 给数据尾部增加换行stu_data \n# 清空stu_info列表stu_info.clear()# 将学生信息写入文件中stus_file.write(stu_data)def load_students():程序启动从文件中加载学生数据stus_file open(students.txt, r) # 打开文件# 从文件中读取学生数据students_data stus_file.readlines()# 声明全局变量studentsglobal studentsglobal IDmax_id 0for student in students_data:# 去除尾部回车student student[:-1]stu_info student.split(,)# 创建学生信息stu_data {}stu_data[name] stu_info[1]stu_data[score] stu_info[2]# 将学生信息插入到stuents字典中students[int(stu_info[0])] stu_data# 获得最大ID值if int(stu_info[0]) max_id:max_id int(stu_info[0])ID max_iddef show_operations():print(请选择您的操作:)print(1. 添加学生信息)print(2. 删除学生信息)print(3. 显示学生信息)print(4. 退出学生系统)print(--------------)def test():print(欢迎使用学生管理系统v1.0)global flag# 加载学生信息load_students()while flag:# 显示菜单show_operations()operation input(您输入的操作是:)if operation 1:add_student()elif operation 2:del_student()elif operation 3:show_students()elif operation 4:flag Falsesave_students()else:print(操作错误请重新选择操作!)test()