Python實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
本文實(shí)例為大家分享了Python實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
基本功能:
輸入并存儲(chǔ)學(xué)生的信息:通過(guò)輸入學(xué)生的學(xué)號(hào)、姓名、和分?jǐn)?shù),然后就可以把數(shù)據(jù)保存在建立的student文件里面。
打印學(xué)生的所有信息:通過(guò)一個(gè)打印函數(shù)就可以把所有的信息打印在屏幕上。
修改學(xué)生信息:這個(gè)功能首先通過(guò)查詢功能查詢出該學(xué)生是否存在,如果存在就對(duì)該學(xué)生的信息進(jìn)行修改,如果不存在則返回到主界面。
刪除學(xué)生信息:該功能是對(duì)相應(yīng)的學(xué)生進(jìn)行刪除操作,如果學(xué)生存在就查找到進(jìn)行刪除。
按學(xué)生成績(jī)進(jìn)行排序: 這個(gè)功能是按照學(xué)生的成績(jī)進(jìn)行排序,對(duì)學(xué)生的信息進(jìn)行操作。
查找學(xué)生信息:這個(gè)功能通過(guò)輸入學(xué)號(hào),查找該學(xué)生的信息,如果有該學(xué)號(hào)就輸出該學(xué)生的信息,沒(méi)有該學(xué)號(hào)就提示輸入的學(xué)號(hào)不存在。
初始化功能
系統(tǒng)在開(kāi)始使用之前先進(jìn)行初始化功能,判斷students.txt文件中是否保存的有學(xué)生的信息,如果有就把文件的內(nèi)容讀取出來(lái),供接下來(lái)的操作使用,如用沒(méi)有就初始化一個(gè)空的列表,用來(lái)保存用戶的輸入,程序中接下來(lái)的所有數(shù)據(jù)都會(huì)保存在該列表中相當(dāng)與一個(gè)數(shù)據(jù)緩沖區(qū)。
首先是打開(kāi)文件操作,對(duì)文件中的內(nèi)容進(jìn)行讀取操作,由于在文件中保存的內(nèi)容是由空格進(jìn)行分割的,并且每一個(gè)學(xué)生的信息都占用一行,首先讀出所有的內(nèi)容,先進(jìn)行按照換行進(jìn)行分割,得到每個(gè)人的信息,然后再對(duì)每個(gè)人的信息進(jìn)行安裝空格分隔,得到每個(gè)人的詳細(xì)信息包括用戶的姓名,學(xué)號(hào),成績(jī)。
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 "初始化成功!"
成績(jī)排序?qū)崿F(xiàn)
這部分代碼是按照學(xué)生成績(jī)的高低進(jìn)行排序,在實(shí)現(xiàn)的時(shí)候,首先是把所有人的成績(jī)放到一個(gè)列表里面然后使用插入排序,按照成績(jī)的大小對(duì)StuList中保存的學(xué)生信息的地址進(jìn)行排序
def Sort(stulist): #按學(xué)生成績(jī)排序 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é)號(hào)查找看是否學(xué)號(hào)已經(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é)號(hào)已經(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é)號(hào) 姓名 語(yǔ)文 數(shù)學(xué) 英語(yǔ) 總分"
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 "沒(méi)有該學(xué)生學(xué)號(hào)!"
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 "沒(méi)有該學(xué)生學(xué)號(hào)!"
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("請(qǐng)輸入學(xué)生的姓名")
while True:
stu.ID = raw_input("請(qǐng)輸入學(xué)生的ID")
p = re.compile('^[0-9]{3}$')
if p.match(stu.ID):
break
else:
print "輸入的有錯(cuò)誤!"
while True:
stu.score1 = int(raw_input("請(qǐng)輸入學(xué)生語(yǔ)文成績(jī)"))
if stu.score1 <= 100 and stu.score1 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
while True:
stu.score2 = int(raw_input("請(qǐng)輸入學(xué)生數(shù)學(xué)成績(jī)"))
if stu.score2 <= 100 and stu.score2 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
while True:
stu.score3 = int(raw_input("請(qǐng)輸入學(xué)生英語(yǔ)成績(jī)"))
if stu.score3 <= 100 and stu.score3 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
stu.sum = stu.score1 + stu.score2 + stu.score3
Add(stulist,stu)
def display(stulist): #顯示所有學(xué)生信息
print u"學(xué)號(hào) 姓名 語(yǔ)文 數(shù)學(xué) 英語(yǔ) 總分"
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é)生成績(jī)排序
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("請(qǐng)輸入你的選擇:")
if nChoose == "1":
stu = Student()
stu.name = raw_input("請(qǐng)輸入學(xué)生的姓名")
while True:
stu.ID = raw_input("請(qǐng)輸入學(xué)生的ID")
p = re.compile('^[0-9]{3}$')
if p.match(stu.ID):
break
else:
print "輸入的有錯(cuò)誤!"
while True:
stu.score1 = int(raw_input("請(qǐng)輸入學(xué)生語(yǔ)文成績(jī)"))
if stu.score1 <= 100 and stu.score1 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
while True:
stu.score2 = int(raw_input("請(qǐng)輸入學(xué)生數(shù)學(xué)成績(jī)"))
if stu.score2 <= 100 and stu.score2 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
while True:
stu.score3 = int(raw_input("請(qǐng)輸入學(xué)生英語(yǔ)成績(jī)"))
if stu.score3 <= 100 and stu.score3 > 0 :
break
else:
print "輸入的學(xué)生成績(jī)有錯(cuò)誤!"
stu.sum = stu.score1 + stu.score2 + stu.score3
Add(stulist,stu)
if nChoose == '2':
ID = raw_input("請(qǐng)輸入學(xué)生的ID")
Search(stulist, ID)
if nChoose == '3':
ID = raw_input("請(qǐng)輸入學(xué)生的ID")
Del(stulist, ID)
if nChoose == '4':
ID = raw_input("請(qǐng)輸入學(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í)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開(kāi)發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
巧妙使用python?opencv庫(kù)玩轉(zhuǎn)視頻幀率
這篇文章主要介紹了巧妙使用python?opencv庫(kù)玩轉(zhuǎn)視頻幀率的教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
將Dataframe數(shù)據(jù)轉(zhuǎn)化為ndarry數(shù)據(jù)的方法
今天小編就為大家分享一篇將Dataframe數(shù)據(jù)轉(zhuǎn)化為ndarry數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
在windows系統(tǒng)中實(shí)現(xiàn)python3安裝lxml
本文主要給大家簡(jiǎn)單介紹了下在windows以及l(fā)inux系統(tǒng)中使用Python安裝LXML模塊的教程,非常簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下2016-03-03
python封裝json格式字符串并處理單雙引號(hào)問(wèn)題
大家好,本篇文章主要講的是python封裝json格式字符串并處理單雙引號(hào)問(wèn)題,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Python把對(duì)應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲(chǔ)腳本的方法
今天小編就為大家分享一篇Python把對(duì)應(yīng)格式的csv文件轉(zhuǎn)換成字典類型存儲(chǔ)腳本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Python利用scapy實(shí)現(xiàn)ARP欺騙的方法
今天小編就為大家分享一篇Python利用scapy實(shí)現(xiàn)ARP欺騙的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
Python實(shí)現(xiàn)免費(fèi)音樂(lè)下載器
本文主要為大家介紹了通過(guò)Python實(shí)現(xiàn)的免費(fèi)音樂(lè)下載器,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的小伙伴可以學(xué)習(xí)一下2021-12-12

