python实现学生管理系统实现学生管理系统
python写的简单的学生管理系统,练习python语法。
可以运行在windows和linux下,python 2.7。
#!/usr/local/bin/python
# -*- coding:utf-8 -*-
import os
import re
#定义学生类
class Student:
def __init__(self):
self.name = ''
self.ID = ''
self.score = 0
#根据学生分数进行从大到小的冒泡排序
def BuddleSortByScore( stulist ):
n = len( stulist )
for i in range( n ):
for j in range( n - i - 1):
if stulist[j].score < stulist[j+1].score:
#tmp = stulist[j+1] #stulist[j+1] = stulist[j] #stulist[j] = tmp
stulist[j],stulist[j+1] = stulist[j+1],stulist[j]
#按顺序输出所有学生的信息
def PrintAllStudentInfo( stulist ):
print u"______________________学生列表_______________"
for i in range( len(stulist) ):
print u"姓名:" ,
print stulist[i].name,
print " " ,
print u"学号:" ,
print stulist[i].ID ,
print " " ,
print u"分数:" ,
print stulist[i].score
print "____________________________________________"
#增加一个学生,增加成功返回True,否则返回False
def Add( stulist , stu ):
if searchByID( stulist , stu.ID ) == 1:
print u"此ID已经存在!"
return False
stulist.append( stu )
#给出是否保存更新数据的选择
print "Do you want to save the result ?"
nChoose = raw_input("Choose:Y/N:")
if nChoose == 'Y' or nChoose == 'y':
#将改变后的结果写入文件中,追加写文件
file_object = open("students.txt","a")
file_object.write( stu.ID )
file_object.write( " " )
file_object.write( stu.name )
file_object.write( " " )
file_object.write( str(stu.score) )
file_object.write( "" )
file_object.close()
return True
else:
stulist.remove(stu)
#根据ID删除一个学生的信息,删除成功则返回True,否则返回false