python信息管理系統
Ⅰ 用Python編寫班級檔案管理系統
# -*- coding: cp936 -*-
class StuInfo:
def __init__(self):
self.Stu=[{"Sno":"1","Sname":"姓名","ChineseScore":64,"MathsScore":34,"EnglishScore":94,"ComputerScore":83},
{"Sno":"2","Sname":"姓名","ChineseScore":44,"MathsScore":24,"EnglishScore":44,"ComputerScore":71},
{"Sno":"3","Sname":"姓名","ChineseScore":74,"MathsScore":35,"EnglishScore":74,"ComputerScore":93},
{"Sno":"4","Sname":"姓名","ChineseScore":94,"MathsScore":54,"EnglishScore":24,"ComputerScore":73}]
self.attribute={"Sno":"學號",
"Sname":"姓名",
"ChineseScore":"語文成績",
"MathsScore":"數學成績",
"EnglishScore":"英語成績",
"ComputerScore":"計算機成績"
}
def _add(self):
'''添加'''
singleInfo={}
for i in self.attribute:
if "Score" in i:
singleInfo[i]=int(raw_input(self.attribute[i]+"\n"))
else:
singleInfo[i]=raw_input(self.attribute[i]+"\n").strip()
self.Stu.append(singleInfo)
print "添加成功OK"
for i in singleInfo:
print i,"=",singleInfo[i]
def _del(self):
"""刪除學號為Sno的記錄"""
Sno=raw_input("學號:\n")
self.Stu.remove(self.__getInfo(Sno))
print "刪除成功OK"
def _update(self):
"""更新數據"""
Sno=raw_input("學號\n").strip()
prefix="修改"
updateOperate={"1":"ChineseScore",
"2":"MathsScore",
"3":"EnglishScore",
"4":"ComputerScore"}
for i in updateOperate:
print i,"-->",prefix+self.attribute[updateOperate[i]]
getOperateNum=raw_input("選擇操作:\n")
if getOperateNum:
getNewValue=int(raw_input("輸入新的值:\n"))
record=self.__getInfo(Sno)
record[updateOperate[getOperateNum]]=getNewValue
print "修改"+record["Sname"]+"的"+str(updateOperate[getOperateNum])+"成績=",getNewValue,"\n成功OK"
def _getInfo(self):
"""查詢數據"""
while True:
print "1->學號查詢 2->條件查詢 3->退出"
getNum=raw_input("選擇:\n")
if getNum=="1":
Sno=raw_input("學號:\n")
print filter(lambda record:record["Sno"]==Sno,self.Stu)[0]
elif getNum=="2":
print "ChineseScore 語文成績;","MathsScore 數學成績;","EnglishScore 英語成績;","ComputerScore 計算機成績;"
print "等於 ==,小於 <, 大於 > ,大於等於 >=,小於等於<= ,不等於!="
print "按如下格式輸入查詢條件 eg: ChineseScore>=60 "
expr=raw_input("條件:\n")
笭工蒂繼酈荒墊維叮哩 Infos=self.__getInfo(expr=expr)
if Infos:
print "共%d記錄"%len(Infos)
for i in Infos:
print i
else:
print "記錄為空"
elif getNum=="3":
break
else:
pass
def __getInfo(self,Sno=None,expr=""):
"""查詢數據
根據學號 _getInfo("111111")
根據分數 _getInfo("EnglishSorce>80")"""
if Sno:
return filter(lambda record:record["Sno"]==Sno,self.Stu)[0]
for operate in [">=",">","<=","<","==","!="]:
if operate in expr:
gradeName,value=expr.split(operate)
return filter(lambda record: eval( repr(record[gradeName.strip()])+operate+value.strip()) ,self.Stu)
return {}
def _showAll(self):
"""顯示所有記錄"""
for i in self.Stu:
print i
@staticmethod
def test():
"""測試"""
_StuInfo=StuInfo()
while True:
print "1->錄入數據 2->修改數據 3->刪除數據 4->查詢數據 5->查看數據 6->退出"
t=raw_input("選擇:\n")
if t=="1":
print "錄入數據"
_StuInfo._add()
elif t=="2":
print "修改數據"
_StuInfo._update()
elif t=="3":
print "刪除數據"
_StuInfo._del()
elif t=="4":
print "查詢數據"
_StuInfo._getInfo()
elif t=="5":
print "顯示所有記錄"
_StuInfo._showAll()
elif t=="6":
break
else:
pass
if __name__=="__main__":
StuInfo.test()