python面向對象之學生管理系統
話不多說,先看看代碼(求關注+贊,努力寫出好的東西!…_ 謝謝!!)
#哪裏有所不妥可以修改
#python面向對象之學生管理系統
import sys
class StudentManage:
student_list = []
#添加學生信息
def rank_score(self):
b = int(input('降序還是逆序?'))
self.student_list.sort(key = lambda x:int(x[5]),reverse = b)
def rank_number(self):
c = int(input('將序還是升序?'))
self.student_list.sort(key = lambda x:int(x[1]),reverse = c)
def print_students(self):
title_old = ['學號','姓名','python','數學','英語','總分']
print('\t'.join(title_old))
for each_list in self.student_list:
print('\t'.join(each_list))
def creat_new(self):
with open('new.score.txt','w') as new_F:
title_new = ['學號','姓名','python','數學','英語','總分']
new_F.write('\t'.join(title_new)+'\n')
for each_new in self.student_list:
new_F.write('\t'.join(each_new)+'\n')
print('創建新的成績單成功!!!')
def add_student(self):
new_student = []
self.number = input('請輸入姓名:')
self.name = input('請輸入學號:')
self.python = input('請輸入python的分數:')
self.math = input('請輸入數學的分數:')
self.English =input('請輸入英語的分數:')
new_student.append(self.number)
new_student.append(self.name)
new_student.append(self.python)
new_student.append(self.math)
new_student.append(self.English)
new_student.append(str(int(self.python)+int(self.math)+int(self.English)))
self.student_list.append(new_student)
def read_in(self):
with open('score.txt') as F:
#一般文件的第一行爲標題行,我們先不考慮
F.readline() #移動文件指針到第一行末尾
for each_line in F:
read_new = []
read_new = each_line.strip('\n').split('\t') #之間的間隔都是tab
read_new.append(str(int(read_new[2])+int(read_new[3])+int(read_new[4])))
self.student_list.append(read_new)
def menu(self):
while True:
s1 = '某某大學學生管理系統'
s2 = '1.讀入已有學生信息'
s3 = '2.添加學生信息'
s4 = '3.以總成績排序(1降序,0升序)'
s5 = '4.以學號排序(1降序,0升序)' #總分
s8 = '5.打印當前學生信息'
s6 = '6.存入一個新的文件中'
s7 = '7.退出'
print(s1.center(20,'-'),s2.ljust(20),s3.ljust(20),s4.ljust(20),s5.ljust(20),s6.ljust(20),s8.ljust(20),s7.ljust(20),sep = '\n')
choice = int(input('CHOICE:'))
if choice == 1:
self.read_in()
self.print_students()
if choice == 2:
self.add_student()
self.print_students()
if choice == 3:
self.rank_score()
self.print_students()
if choice == 6:
self.creat_new()
if choice == 5:
self.print_students()
if choice == 7:
sys.exit(0)
if choice == 4:
self.rank_number()
self.print_students()
aa = StudentManage()
print(aa.menu())
效果圖,比不上什麼大流,對於初學者應該算不錯的了,(都能看懂吧應該!)
_ 關注一下吧,努力寫出好東西