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( "/r/n" ) file_object.close() return True else: stulist.remove(stu) #根據ID刪除一個學生的信息,刪除成功則返回True,否則返回false def DeleteByID( stulist , ID ): for stu in stulist: if stu.ID == ID: stulist.remove( stu ) #選擇是否保存已經改變的內容 print "Do you want to save the changed result ?" nChoose = raw_input("Choose:Y/N:") if nChoose == 'Y' or nChoose == 'y': file_object = open("students.txt" , "w+") for stu2 in stulist: file_object.write(stu2.ID) file_object.write(" ") file_object.write(stu2.name) file_object.write(" ") file_object.write(str(stu2.score)) file_object.write("/r/n") file_object.close() print u"刪除成功!" return True print u"刪除失敗!" return False #根據姓名刪除一個學生的信息,刪除成功返回True,否則返回False def DeleteByName( stulist , name ): pos = searchByName( stulist , name ) if pos != -1: del stulist[pos] #選擇是否保存已經改變的內容 print "Do you want to save the changed result ?" nChoose = raw_input("Choose:Y/N:") if nChoose == 'Y' or nChoose == 'y': file_object = open("students.txt" , "w+") for stu2 in stulist: file_object.write(stu2.ID) file_object.write(" ") file_object.write(stu2.name) file_object.write(" ") file_object.write(str(stu2.score)) file_object.write("/r/n") file_object.close() print u"刪除成功!" return True else: print u"刪除失敗!" print pos return False #根據學號查找一個學生是否存在,存在返回學生在列表中的下標,否則返回-1 def searchByID( stulist , ID ): for i in range( len(stulist) ): if stulist[i].ID == ID: print u"姓名:" , print stulist[i].name , print " " , print u"學號:" , print stulist[i].ID , print " " , print u"分數:" , print stulist[i].score return i return -1 #根據姓名查找一個學生是否存在,存在返回學生在列表中的下標,否則返回-1 def searchByName( stulist , name ): for i in range( len(stulist) ): if stulist[i].name == name: print u"姓名:" , print stulist[i].name , print " " , print u"學號:" , print stulist[i].ID , print " " , print u"分數:" , print stulist[i].score return i return -1 #初始化,讀取文件,更新學生信息 def Init( stulist ): print u"初始化......" file_object = open("students.txt","r") #按行讀取文件中學生的信息 for line in file_object: stu = Student() line = line.strip("/n") s = line.split(" ") stu.ID = s[0] stu.name = s[1] stu.score = s[2] stulist.append(stu) print u"初始化成功!" #查找菜單 def QueryMenu( stulist ): while True: print "******************************" print u"根據學生的學號進行查找-------1" print u"根據學生的姓名進行查找-------2" print u"離開查找模塊----------------3" print "******************************" nChoose = raw_input("請輸入你的選擇") if nChoose == "1": ID = raw_input("請輸入你要輸入查找的ID:") searchByID( stulist , ID ) elif nChoose == "2": name = raw_input("請輸入你要查找的姓名:") searchByName( stulist , name ) elif nChoose == "3": return else: print u"選擇輸入錯誤,請重新輸入!" #刪除模塊 def DeleteMenu( stulist ): while True: print "*****************************" print u"根據學生的學號進行刪除------1" print u"根據學生的姓名進行刪除------2" print u"離開刪除模塊---------------3" print "******************************" nChoose = raw_input("請進行選擇:") if nChoose == "1": ID = raw_input("請輸入你要刪除的ID:") DeleteByID( stulist , ID ) elif nChoose == "2": name = raw_input("請輸入你要刪除的姓名:") DeleteByName( stulist , name ) elif nChoose == "3": return else: print u"您的選擇有誤,請重新輸入!" #菜單 def menu( stulist ): while True: print "***********************" print u"--------菜單------------" print u"增加學生信息---------1" print u"查找一個學生的信息----2" print u"刪除一個學生的信息----3" print u"輸出所有學生的信息----4" print u"根據分數排序---------5" print u"退出程序-------------6" print "------------------------" print "************************" nChoose = raw_input("請輸入你的選擇:") if nChoose == "1": stu = Student() stu.name = raw_input("請輸入學生的姓名:") #匹配學生ID是否滿足0--9中的數字 while True: stu.ID = raw_input("請輸入學生的ID:") #創建編譯一個正則表達式的模板 p = re.compile( '^[0-9]{3}$' ) if p.match( stu.ID ): break else: print "學生的ID輸入錯誤,正確形式應該為0--9之間的三位數字!" #匹配學生的分數是否滿足0--100之間 while True: stu.score = eval( raw_input("請輸入學生的分數:") ) #利用普通的符號來判斷分數是否符合標準 #if stu.score >= 0 and stu.score <= 100: # break #利用正則表達式來判斷分數是否符合標準 if re.match( "^[0-9]" ,str(stu.score) ) and stu.score<=100 and stu.score >= 0 : break else: print u"分數不滿足實際情況,應該為0--100之間的數字!" if Add( stulist , stu ) == 1: print u"學生信息增加成功!" else: print u"學生信息增加失敗!" elif nChoose == "2": QueryMenu( stulist ) elif nChoose == "3": DeleteMenu( stulist ) elif nChoose == "4": PrintAllStudentInfo( stulist ) elif nChoose == "5": BuddleSortByScore( stulist ) print "Do you want to save the sorted result?" choose = raw_input("please input your choice:Y/N:") if choose == 'Y' or choose == 'y': file_object = open("students.txt","w+") for stu2 in stulist: file_object.write(stu2.ID) file_object.write(" ") file_object.write(stu2.name) file_object.write(" ") file_object.write(str(stu2.score)) file_object.write("/r/n") elif nChoose == "6": return else: print u"輸入有誤,請重新輸入!" #測試函數部分 if __name__ == '__main__': #定義一個列表用來存儲所有學生的信息 stulist = [] #初始化,從文件中讀取信息 Init( stulist ) #菜單函數 menu( stulist )
新聞熱點
疑難解答