天天看點

python學生成績管理系統實驗報告_Python實作學生成績管理系統

本文執行個體為大家分享了Python實作學生成績管理系統的具體代碼,供大家參考,具體内容如下

基本功能:

輸入并存儲學生的資訊:通過輸入學生的學号、姓名、和分數,然後就可以把資料儲存在建立的student檔案裡面。

列印學生的所有資訊:通過一個列印函數就可以把所有的資訊列印在螢幕上。

修改學生資訊:這個功能首先通過查詢功能查詢出該學生是否存在,如果存在就對該學生的資訊進行修改,如果不存在則傳回到主界面。

删除學生資訊:該功能是對相應的學生進行删除操作,如果學生存在就查找到進行删除。

按學生成績進行排序: 這個功能是按照學生的成績進行排序,對學生的資訊進行操作。

查找學生資訊:這個功能通過輸入學号,查找該學生的資訊,如果有該學号就輸出該學生的資訊,沒有該學号就提示輸入的學号不存在。

初始化功能

系統在開始使用之前先進行初始化功能,判斷students.txt檔案中是否儲存的有學生的資訊,如果有就把檔案的内容讀取出來,供接下來的操作使用,如用沒有就初始化一個空的清單,用來儲存使用者的輸入,程式中接下來的所有資料都會儲存在該清單中相當與一個資料緩沖區。

首先是打開檔案操作,對檔案中的内容進行讀取操作,由于在檔案中儲存的内容是由空格進行分割的,并且每一個學生的資訊都占用一行,首先讀出所有的内容,先進行按照換行進行分割,得到每個人的資訊,然後再對每個人的資訊進行安裝空格分隔,得到每個人的詳細資訊包括使用者的姓名,學号,成績。

def Init(stulist): #初始化函數

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 "初始化成功!"

成績排序實作

這部分代碼是按照學生成績的高低進行排序,在實作的時候,首先是把所有人的成績放到一個清單裡面然後使用插入排序,按照成績的大小對StuList中儲存的學生資訊的位址進行排序

def Sort(stulist): #按學生成績排序

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]

temp = stulist[i]

stulist[i] = stulist[j]

stulist[j] = temp

界面截圖如下:

python學生成績管理系統實驗報告_Python實作學生成績管理系統

源碼:

# -*- coding: UTF-8 -*-

import os

import re

import numpy as np

class Student: #定義一個學生類

def __init__(self):

self.name = ''

self.ID =''

self.score1 = 0

self.score2 = 0

self.score1 = 0

self.sum = 0

def searchByID(stulist, ID): #按學号查找看是否學号已經存在

for item in stulist:

if item.ID == ID:

return True

def Add(stulist,stu): #添加一個學生資訊

if searchByID(stulist, stu.ID) == True:

print"學号已經存在!"

return False

stulist.append(stu)

print stu.name,stu.ID, stu.score1, stu.score2, stu.score3, stu.sum;

print "是否要儲存學生資訊?"

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): #搜尋一個學生資訊

print u"學号 姓名 國文 數學 英語 總分"

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 "沒有該學生學号!"

def Del(stulist, ID): #删除一個學生資訊

count = 0

for item in stulist:

if item.ID == ID:

stulist.remove(item)

print "删除成功!"

break

count +=1

# if count == len(stulist):

# print "沒有該學生學号!"

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("請輸入學生的姓名")

while True:

stu.ID = raw_input("請輸入學生的ID")

p = re.compile('^[0-9]{3}$')

if p.match(stu.ID):

break

else:

print "輸入的有錯誤!"

while True:

stu.score1 = int(raw_input("請輸入學生國文成績"))

if stu.score1 <= 100 and stu.score1 > 0 :

break

else:

print "輸入的學生成績有錯誤!"

while True:

stu.score2 = int(raw_input("請輸入學生數學成績"))

if stu.score2 <= 100 and stu.score2 > 0 :

break

else:

print "輸入的學生成績有錯誤!"

while True:

stu.score3 = int(raw_input("請輸入學生英語成績"))

if stu.score3 <= 100 and stu.score3 > 0 :

break

else:

print "輸入的學生成績有錯誤!"

stu.sum = stu.score1 + stu.score2 + stu.score3

Add(stulist,stu)

def display(stulist): #顯示所有學生資訊

print u"學号 姓名 國文 數學 英語 總分"

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): #按學生成績排序

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]