本文章將實現對於學生成績的簡單管理,下面展示實驗的要求: 1、創建4個.py模塊,在壹個包內,模塊命名分別為main.py,ui.py,model.py,bll.py。
2、(1)實現學生管理系統在控制臺的輸出顯示界面如圖1所示。
(2)在model.py模塊中建立學生類,其中包含的屬性有name,age,score,id。
(3)在bll.py模塊中建立學生管理業務邏輯類,即在此模塊中實現增加、顯示、刪除、修改和成績排序等功能。
(4)在ui.py模塊中建立學生管理顯示界面。
(5)在main.py模塊中實現建立if? name ?== “ main ”:
提示:本程序首先要建立壹個包
壹 . model.py
建立學生類
`class Students(object):
"""對壹個學生的簡單表示"""
def __init__(self,stu_id,stu_name,stu_age,stu_score):
"""設置屬性的初始值"""
self.stu_name = stu_name? #姓名
self.stu_id=stu_id#學號
self.stu_age=stu_age#年齡
self.stu_score=stu_score#分數
def __str__(self):
return '學號:{} 姓名:{} 年齡:{} 成績:{}'.format(self.stu_id,self.stu_name,self.stu_age,self.stu_score)`
二.bll.py
from 成績管理.model import *classStudentManagerController(object): def__init__(self): self.stu_list=[]#定義壹個列表 defadd(self): #1.添加學生信息 flase=0 while flase == 0:
id = int(input("學號:"))
if id>0:
print('重新輸入')
else:
flase=1 name=input("姓名:")
flase = 0 while flase == 0:
age = int(input("年齡:"))
if 0 < age:
flase = True else:
print('重新輸入')
flase = 0 while flase == 0:
score = int(input("成績:"))
if 0 < score:
flase = True else:
print('重新輸入')
flase = 0 stu = Students(id,name,age,score)
self.add_stu(stu)
defshow(self): #2.顯示學生列表 if len(self.stu_list) == 0:
print("當前沒有任何的學生記錄")
self.show_stu()
defdel_stu1(self): #3.刪除學生信息 stu_id = int(input("請輸入學號(刪除):"))
stu_id1=stu_id-1 for stu_id in self.stu_list:
if stu_id in self.stu_list:
self.del_stu(stu_id1)
defrevise1(self): #4.修改學生 num = int(input("請輸入修改學生的位數:"))
num1=num-1 flase = 0 while flase == 0:
id1 = int(input("輸入新的學號:"))
if 0 < id1:
flase = True else:
print('重新輸入')
name1 = input('輸入新的姓名:')
flase = 0 while flase == 0:
age1 = int(input("輸入新的年齡:"))
if 0 < age1:
flase = True else:
print('重新輸入')
flase = 0 while flase == 0:
score1 = int(input("輸入新的分數:"))
if 0 < score1:
flase = True else:
print('重新輸入')
flase = 0 stu1 = Students(id1,name1,age1, score1)
self.stu_list[num1] = stu1
self.show_stu()
defpaixv(self): #5.排序: self.sort_stu(key=lambda s: s.stu_score, reverse=True)? # 按照分數排列 self.show_stu()
defadd_stu(self,stu):#添加 self.stu_list.append(stu)#變量加入到列表末尾 #self.stu_dict[stu.stu_id]=stu#列表中嵌套壹個字典 defdel_stu(self,stu_id):#刪除 #s=self.stu_dict[stu_id] self.stu_list.remove(stu_id)#根據編號刪除學生信息 defsort_stu(self,key=None,reverse=False):#排序 key=lambda? 變量:變量[維數] 。維數可以按照自己的需要進行設置。 self.stu_list.sort(key=key,reverse=reverse)
defrevise_stu(self,stu1):#修改 a=int(input("輸入想要修改學生的位數"))
self.stu_list[a]=stu1
defshow_stu(self):#顯示 for s in self.stu_list:#遍歷列表 print(s)
三.ui.py
from 成績管理.bll import *classStudentManagerView(object): def__init__(self): self.mamager=StudentManagerController()#實例化 defshixian(self): while True:
#show_menu()#調用ui.py print('*' * 20)
print('1)添加學生')
print('2)顯示學生')
print('3)刪除學生')
print('4)修改學生')
print('5)按照成績降序排列')
print('0)退出程序')
print('*' * 20)
key=int(input("請輸入功能:"))
if key in range(0, 6):
if key==1:
self.mamager.add()
elif key==2:
self.mamager.show()
elif key==3:
self.mamager.del_stu1()
elif key==4:
self.mamager.revise1()
elif key==5:
self.mamager.paixv()
else:
print("退出程序")
break else:
print("輸入錯誤!")
四.main.py
from 成績管理.ui import*
if __name__=="__main__":
n=StudentManagerView()
n.shixian()
以上就是案例的簡單實現。想要的可以私信。
原文鏈接:/details/J5rpQ62dDNN287zvBl
SDK社區是壹個中立的社區,這裏有多樣的前端知識,有豐富的api,有愛學習的人工智能開發者,有風趣幽默的開發者帶妳學python,還有未來火熱的鴻蒙,當各種元素組合在壹起,讓我們壹起腦洞大開***同打造專業、好玩、有價值的開發者社區,幫助開發者實現自我價值!