Python實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)
本文實(shí)例為大家分享了Python實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
基本功能:
輸入并存儲學(xué)生的信息:通過輸入學(xué)生的學(xué)號、姓名、和分?jǐn)?shù),然后就可以把數(shù)據(jù)保存在建立的student文件里面。
打印學(xué)生的所有信息:通過一個(gè)打印函數(shù)就可以把所有的信息打印在屏幕上。
修改學(xué)生信息:這個(gè)功能首先通過查詢功能查詢出該學(xué)生是否存在,如果存在就對該學(xué)生的信息進(jìn)行修改,如果不存在則返回到主界面。
刪除學(xué)生信息:該功能是對相應(yīng)的學(xué)生進(jìn)行刪除操作,如果學(xué)生存在就查找到進(jìn)行刪除。
按學(xué)生成績進(jìn)行排序: 這個(gè)功能是按照學(xué)生的成績進(jìn)行排序,對學(xué)生的信息進(jìn)行操作。
查找學(xué)生信息:這個(gè)功能通過輸入學(xué)號,查找該學(xué)生的信息,如果有該學(xué)號就輸出該學(xué)生的信息,沒有該學(xué)號就提示輸入的學(xué)號不存在。
初始化功能
系統(tǒng)在開始使用之前先進(jìn)行初始化功能,判斷students.txt文件中是否保存的有學(xué)生的信息,如果有就把文件的內(nèi)容讀取出來,供接下來的操作使用,如用沒有就初始化一個(gè)空的列表,用來保存用戶的輸入,程序中接下來的所有數(shù)據(jù)都會保存在該列表中相當(dāng)與一個(gè)數(shù)據(jù)緩沖區(qū)。
首先是打開文件操作,對文件中的內(nèi)容進(jìn)行讀取操作,由于在文件中保存的內(nèi)容是由空格進(jìn)行分割的,并且每一個(gè)學(xué)生的信息都占用一行,首先讀出所有的內(nèi)容,先進(jìn)行按照換行進(jìn)行分割,得到每個(gè)人的信息,然后再對每個(gè)人的信息進(jìn)行安裝空格分隔,得到每個(gè)人的詳細(xì)信息包括用戶的姓名,學(xué)號,成績。
def Init(stulist): #初始化函數(shù) print "初始化......" 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 "初始化成功!"
成績排序?qū)崿F(xiàn)
這部分代碼是按照學(xué)生成績的高低進(jìn)行排序,在實(shí)現(xiàn)的時(shí)候,首先是把所有人的成績放到一個(gè)列表里面然后使用插入排序,按照成績的大小對StuList中保存的學(xué)生信息的地址進(jìn)行排序
def Sort(stulist): #按學(xué)生成績排序 Stu = [] sum_count = [] for li in stulist: temp = [] temp.append(li.ID) temp.append(li.name) temp.append(int(li.score1)) temp.append(int(li.score2)) temp.append(int(li.score3)) temp.append(int(li.sum)) sum_count.append(int(li.sum)) Stu.append(temp) #print sum_count #print Stu; #print stulist insertSort(sum_count, stulist) #print stulist; display(stulist) def insertSort(a, stulist): for i in range(len(a)-1): #print a,i for j in range(i+1,len(a)): if a[i]<a[j]: temp = stulist[i] stulist[i] = stulist[j] stulist[j] = temp
界面截圖如下:
源碼:
# -*- coding: UTF-8 -*- import os import re import numpy as np class Student: #定義一個(gè)學(xué)生類 def __init__(self): self.name = '' self.ID ='' self.score1 = 0 self.score2 = 0 self.score1 = 0 self.sum = 0 def searchByID(stulist, ID): #按學(xué)號查找看是否學(xué)號已經(jīng)存在 for item in stulist: if item.ID == ID: return True def Add(stulist,stu): #添加一個(gè)學(xué)生信息 if searchByID(stulist, stu.ID) == True: print"學(xué)號已經(jīng)存在!" return False stulist.append(stu) print stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum; print "是否要保存學(xué)生信息?" 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.score1)) file_object.write(" ") file_object.write(str(stu.score2)) file_object.write(" ") file_object.write(str(stu.score3)) file_object.write(" ") file_object.write(str(stu.sum)) file_object.write("\n") file_object.close() print u"保存成功!" def Search(stulist, ID): #搜索一個(gè)學(xué)生信息 print u"學(xué)號 姓名 語文 數(shù)學(xué) 英語 總分" count = 0 for item in stulist: if item.ID == ID: print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum break count = 0 if count == len(stulist): print "沒有該學(xué)生學(xué)號!" def Del(stulist, ID): #刪除一個(gè)學(xué)生信息 count = 0 for item in stulist: if item.ID == ID: stulist.remove(item) print "刪除成功!" break count +=1 # if count == len(stulist): # print "沒有該學(xué)生學(xué)號!" file_object = open("students.txt", "w") for stu in stulist: print stu.ID, stu.name, stu.score1,stu.score2, stu.score3, stu.sum file_object.write(stu.ID) file_object.write(" ") file_object.write(stu.name) file_object.write(" ") file_object.write(str(stu.score1)) file_object.write(" ") file_object.write(str(stu.score2)) file_object.write(" ") file_object.write(str(stu.score3)) file_object.write(" ") file_object.write(str(stu.sum)) file_object.write("\n") file_object.close() # print "保存成功!" file_object.close() def Change(stulist, ID): count = 0 for item in stulist: if item.ID == ID: stulist.remove(item) file_object = open("students.txt", "w") for stu in stulist: #print li.ID, li.name, li.score file_object.write(stu.ID) file_object.write(" ") file_object.write(stu.name) file_object.write(" ") file_object.write(str(stu.score1)) file_object.write(" ") file_object.write(str(stu.score2)) file_object.write(" ") file_object.write(str(stu.score3)) file_object.write(" ") file_object.write(str(stu.sum)) file_object.write("\n") # print "保存成功!" file_object.close() stu = Student() stu.name = raw_input("請輸入學(xué)生的姓名") while True: stu.ID = raw_input("請輸入學(xué)生的ID") p = re.compile('^[0-9]{3}$') if p.match(stu.ID): break else: print "輸入的有錯(cuò)誤!" while True: stu.score1 = int(raw_input("請輸入學(xué)生語文成績")) if stu.score1 <= 100 and stu.score1 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" while True: stu.score2 = int(raw_input("請輸入學(xué)生數(shù)學(xué)成績")) if stu.score2 <= 100 and stu.score2 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" while True: stu.score3 = int(raw_input("請輸入學(xué)生英語成績")) if stu.score3 <= 100 and stu.score3 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" stu.sum = stu.score1 + stu.score2 + stu.score3 Add(stulist,stu) def display(stulist): #顯示所有學(xué)生信息 print u"學(xué)號 姓名 語文 數(shù)學(xué) 英語 總分" for item in stulist: print item.ID, '\t' ,item.name,'\t', item.score1,'\t',item.score2, '\t', item.score3, '\t',item.sum def Sort(stulist): #按學(xué)生成績排序 Stu = [] sum_count = [] for li in stulist: temp = [] temp.append(li.ID) temp.append(li.name) temp.append(int(li.score1)) temp.append(int(li.score2)) temp.append(int(li.score3)) temp.append(int(li.sum)) sum_count.append(int(li.sum)) Stu.append(temp) #print sum_count #print Stu; #print stulist insertSort(sum_count, stulist) #print stulist; display(stulist) def insertSort(a, stulist): for i in range(len(a)-1): #print a,i for j in range(i+1,len(a)): if a[i]<a[j]: temp = stulist[i] stulist[i] = stulist[j] stulist[j] = temp #return a def Init(stulist): #初始化函數(shù) print "初始化......" 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.score1 = s[2] stu.score2 = s[3] stu.score3 = s[4] stu.sum = s[5] stulist.append(stu) file_object.close() print "初始化成功!" main() def main(): #主函數(shù) 該程序的入口函數(shù) while True: print "*********************" print u"--------菜單---------" print u"增加學(xué)生信息--------1" print u"查找學(xué)生信息--------2" print u"刪除學(xué)生信息--------3" print u"修改學(xué)生信息--------4" print u"所有學(xué)生信息--------5" print u"按照分?jǐn)?shù)排序--------6" print u"退出程序------------0" print "*********************" nChoose = raw_input("請輸入你的選擇:") if nChoose == "1": stu = Student() stu.name = raw_input("請輸入學(xué)生的姓名") while True: stu.ID = raw_input("請輸入學(xué)生的ID") p = re.compile('^[0-9]{3}$') if p.match(stu.ID): break else: print "輸入的有錯(cuò)誤!" while True: stu.score1 = int(raw_input("請輸入學(xué)生語文成績")) if stu.score1 <= 100 and stu.score1 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" while True: stu.score2 = int(raw_input("請輸入學(xué)生數(shù)學(xué)成績")) if stu.score2 <= 100 and stu.score2 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" while True: stu.score3 = int(raw_input("請輸入學(xué)生英語成績")) if stu.score3 <= 100 and stu.score3 > 0 : break else: print "輸入的學(xué)生成績有錯(cuò)誤!" stu.sum = stu.score1 + stu.score2 + stu.score3 Add(stulist,stu) if nChoose == '2': ID = raw_input("請輸入學(xué)生的ID") Search(stulist, ID) if nChoose == '3': ID = raw_input("請輸入學(xué)生的ID") Del(stulist, ID) if nChoose == '4': ID = raw_input("請輸入學(xué)生的ID") Change(stulist, ID) if nChoose == '5': display(stulist) if nChoose == '6': Sort(stulist) if nChoose == '0': break if __name__ == '__main__': stulist =[] Init(stulist)
更多學(xué)習(xí)資料請關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django3.0 異步通信初體驗(yàn)(小結(jié))
這篇文章主要介紹了Django3.0 異步通信初體驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài)
這篇文章介紹了Python面向?qū)ο蟮娜筇匦苑庋b、繼承、多態(tài),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標(biāo)軸設(shè)置)
這篇文章主要介紹了matplotlib常見函數(shù)之plt.rcParams、matshow的使用(坐標(biāo)軸設(shè)置),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01利用python如何實(shí)現(xiàn)貓捉老鼠小游戲
這篇文章主要給大家介紹了關(guān)于利用python如何實(shí)現(xiàn)貓捉老鼠小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Python網(wǎng)絡(luò)編程之xmlrpc模塊
這篇文章介紹了Python網(wǎng)絡(luò)編程之xmlrpc模塊,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05