您好,登錄后才能下訂單哦!
python寫的簡單的學(xué)生管理系統(tǒng),練習(xí)python語法。
可以運(yùn)行在windows和linux下,python 2.7。
#!/usr/local/bin/python # -*- coding:utf-8 -*- import os import re #定義學(xué)生類 class Student: def __init__(self): self.name = '' self.ID = '' self.score = 0 #根據(jù)學(xué)生分?jǐn)?shù)進(jìn)行從大到小的冒泡排序 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] #按順序輸出所有學(xué)生的信息 def PrintAllStudentInfo( stulist ): print u"______________________學(xué)生列表_______________" for i in range( len(stulist) ): print u"姓名:" , print stulist[i].name, print " " , print u"學(xué)號:" , print stulist[i].ID , print " " , print u"分?jǐn)?shù):" , print stulist[i].score print "____________________________________________" #增加一個(gè)學(xué)生,增加成功返回True,否則返回False def Add( stulist , stu ): if searchByID( stulist , stu.ID ) == 1: print u"此ID已經(jīng)存在!" return False stulist.append( stu ) #給出是否保存更新數(shù)據(jù)的選擇 print "Do you want to save the result ?" nChoose = raw_input("Choose:Y/N:") if nChoose == 'Y' or nChoose == 'y': #將改變后的結(jié)果寫入文件中,追加寫文件 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) #根據(jù)ID刪除一個(gè)學(xué)生的信息,刪除成功則返回True,否則返回false def DeleteByID( stulist , ID ): for stu in stulist: if stu.ID == ID: stulist.remove( stu ) #選擇是否保存已經(jīng)改變的內(nèi)容 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 #根據(jù)姓名刪除一個(gè)學(xué)生的信息,刪除成功返回True,否則返回False def DeleteByName( stulist , name ): pos = searchByName( stulist , name ) if pos != -1: del stulist[pos] #選擇是否保存已經(jīng)改變的內(nèi)容 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 #根據(jù)學(xué)號查找一個(gè)學(xué)生是否存在,存在返回學(xué)生在列表中的下標(biāo),否則返回-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"學(xué)號:" , print stulist[i].ID , print " " , print u"分?jǐn)?shù):" , print stulist[i].score return i return -1 #根據(jù)姓名查找一個(gè)學(xué)生是否存在,存在返回學(xué)生在列表中的下標(biāo),否則返回-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"學(xué)號:" , print stulist[i].ID , print " " , print u"分?jǐn)?shù):" , print stulist[i].score return i return -1 #初始化,讀取文件,更新學(xué)生信息 def Init( stulist ): print u"初始化......" file_object = open("students.txt","r") #按行讀取文件中學(xué)生的信息 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"根據(jù)學(xué)生的學(xué)號進(jìn)行查找-------1" print u"根據(jù)學(xué)生的姓名進(jìn)行查找-------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"選擇輸入錯(cuò)誤,請重新輸入!" #刪除模塊 def DeleteMenu( stulist ): while True: print "*****************************" print u"根據(jù)學(xué)生的學(xué)號進(jìn)行刪除------1" print u"根據(jù)學(xué)生的姓名進(jìn)行刪除------2" print u"離開刪除模塊---------------3" print "******************************" nChoose = raw_input("請進(jìn)行選擇:") 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"增加學(xué)生信息---------1" print u"查找一個(gè)學(xué)生的信息----2" print u"刪除一個(gè)學(xué)生的信息----3" print u"輸出所有學(xué)生的信息----4" print u"根據(jù)分?jǐn)?shù)排序---------5" print u"退出程序-------------6" print "------------------------" print "************************" nChoose = raw_input("請輸入你的選擇:") if nChoose == "1": stu = Student() stu.name = raw_input("請輸入學(xué)生的姓名:") #匹配學(xué)生ID是否滿足0--9中的數(shù)字 while True: stu.ID = raw_input("請輸入學(xué)生的ID:") #創(chuàng)建編譯一個(gè)正則表達(dá)式的模板 p = re.compile( '^[0-9]{3}$' ) if p.match( stu.ID ): break else: print "學(xué)生的ID輸入錯(cuò)誤,正確形式應(yīng)該為0--9之間的三位數(shù)字!" #匹配學(xué)生的分?jǐn)?shù)是否滿足0--100之間 while True: stu.score = eval( raw_input("請輸入學(xué)生的分?jǐn)?shù):") ) #利用普通的符號來判斷分?jǐn)?shù)是否符合標(biāo)準(zhǔn) #if stu.score >= 0 and stu.score <= 100: # break #利用正則表達(dá)式來判斷分?jǐn)?shù)是否符合標(biāo)準(zhǔn) if re.match( "^[0-9]" ,str(stu.score) ) and stu.score<=100 and stu.score >= 0 : break else: print u"分?jǐn)?shù)不滿足實(shí)際情況,應(yīng)該為0--100之間的數(shù)字!" if Add( stulist , stu ) == 1: print u"學(xué)生信息增加成功!" else: print u"學(xué)生信息增加失敗!" 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"輸入有誤,請重新輸入!" #測試函數(shù)部分 if __name__ == '__main__': #定義一個(gè)列表用來存儲所有學(xué)生的信息 stulist = [] #初始化,從文件中讀取信息 Init( stulist ) #菜單函數(shù) menu( stulist )
運(yùn)行需要讀寫文件Students.txt。文件格式如下圖:
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。