# -*-coding:utf8-*-
# 學生管理系統完整版
# 學生信息管理項目,要求帶操作界面,并完成每項操作:
# +----------------------+
# | 1)添加學生信息 |
# | 2)顯示所有學生的信息 |
# | 3)刪除學生信息 |
# | 4)修改學生信息 |
# | 5)按學生成績高-低顯示學生信息 |
# | 6)按學生成績低-高顯示學生信息 |
# | 7)按學生年齡高-低顯示學生信息 |
# | 8)按學生年齡低-高顯示學生信息 |
# | 9)保存學生信息到文件(students.txt) |
# | 10)從文件中讀取數據(students.txt) |
# | 退出:其他任意按鍵<回車> |
# +----------------------+
def menu():
menu_info = '''+----------------------+
| 1)添加學生信息 |
| 2)顯示所有學生的信息 |
| 3)刪除學生信息 |
| 4)修改學生信息 |
| 5)按學生成績高-低顯示學生信息 |
| 6)按學生成績低-高顯示學生信息 |
| 7)按學生年齡高-低顯示學生信息 |
| 8)按學生年齡低-高顯示學生信息 |
| 9)保存學生信息到文件(students.txt) |
| 10)從文件中讀取數據(students.txt) |
| 退出:其他任意按鍵<回車> |
+----------------------+
'''
print(menu_info)
# 以下兩個函數用于sorted排序,key的表達式函數。
def get_age(*l):
for x in l:
return x.get('age') # get() 函數返回指定鍵的值,如果值不在字典中返回默認值。
def get_score(*l):
for x in l:
return x.get('score')
# 1)添加學生信息
def addstudentinfo():
l = []
while True:
n = input('請輸入名字:')
if not n: # 名字為空,跳出循環
break
try:
a = int(input('請輸入年齡:'))
s = int(input('請輸入成績:'))
except:
print('輸入無效,不是整型數值,重新輸入信息')
continue
info = {'name': n, 'age': a, 'score': s}
l.append(info)
print('學生信息錄入完畢')
print(l)
return l
# 2)顯示所有學生信息
def showstudentinfo(studentinfo):
if not studentinfo:
print('無數據信息')
return
print('名字'.center(8),'年齡'.center(4),'成績'.center(4))
for info in studentinfo:
print(info.get('name').center(10), str(info.get('age')).center(4), str(info.get('score')).center(4))
#3)刪除學生信息
def delstudentinfo(studentinfo):
delname=input('請輸入刪除的學生姓名:')
for info in studentinfo:
if delname==info.get('name'):#get() 函數返回指定鍵的值
return info
raise IndexError('學生信息不匹配沒有找到%s'%delname)
#4)修改學生信息
def modifystudentinfo(studentinfo):
modifyname=input('請輸入要修改的姓名:')
for info in studentinfo:
if modifyname==info.get('name'):
print(info)
studentinfo.remove(info)
name = input('請輸入要修改后姓名:')
a=int(input('請輸入年齡:'))
s=int(input('請輸入成績:'))
info={'name':name,'age':a,'score':s}
studentinfo.append(info)
return info
raise IndexError('學生信息不匹配沒有找到%s' % modifyname)
#5)按學生成績高-低顯示學生信息
def scorereduce(studentinfo):
print('按學生成績高-低顯示:')
mit=sorted(studentinfo,key=lambda x:x['score'],reverse=True)
showstudentinfo(mit)
#6)按學生成績低-高顯示學生信息
def scorerise(studentinfo):
print('按學生成績低-高顯示:')
mit=sorted(studentinfo,key=lambda x:x['score'])
showstudentinfo(mit)
#7)按學生年齡高-低顯示學生信息
def agereduce(studentinfo):
print('按學生年齡高-低顯示學生信息')
mit=sorted(studentinfo,key=lambda x:x['age'],reverse=True)
showstudentinfo(mit)
#8)按學生年齡低-高顯示學生信息
def agerise(studentinfo):
print('按學生年齡低-高顯示學生信息')
mit=sorted(studentinfo,lambda x:x['age'])
showstudentinfo(mit)
#9)保存學生信息到文件(student.txt)
def saveinfo(studentinfo):
try:
studenttxt=open('student.txt','w')#以寫模式打開,并清空文件內容
except Exception as e:
studenttxt=open('student.txt','x')#文件不存在,創建文件并打開
for info in studentinfo:
studenttxt.write(str(info)+'\n')
print('保存成功')
studenttxt.close()
#10)從文件中讀取數據(student.txt)
def readinfo():
oldinfo=[]
try:
studenttxt=open('student.txt')
except:
print('暫未保存數據信息')
return
while True:
info=studenttxt.readline()
if not info:
break
print('讀取的數據信息',info)
info=info.rstrip()#掉換行符
print('rstrip', info)
info=info[1:-1]#去掉{}
print('info[1:-1]',info)
studentdict={}#單個學生字典信息
for x in info.split(','):#以,為間隔分拆分
print('x',x)
keyvalue=[]#開辟空間,key_value[0]存key,key_value[0]存value
for k in x.split(':'):#以:為間隔拆分
k=k.strip()#去掉首尾空字符
print('key',k)
if k[0]==k[-1] and len(k)>2:#判斷是字符串還是整數
keyvalue.append(k[1:-1])#去掉首尾'
print('str',keyvalue)
else:
keyvalue.append(int(k))
print('keyvalue',keyvalue)
print('keyvalue[0]',keyvalue[0])
print('keyvalue[1]',keyvalue[1])
studentdict[keyvalue[0]]=keyvalue[1]#學生信息添加
print('studentdict',studentdict)
oldinfo.append(studentdict)#所有學生信息匯總
print('讀取成功')
studenttxt.close()
return oldinfo
def main():
studentinf=[]
while True:
menu()
number=input('請輸入選項:')
if number=='1':
studentinfo=addstudentinfo()
elif number=='2':
showstudentinfo(studentinfo)
elif number=='3':
try:
studentinfo.remove(delstudentinfo(studentinfo))
except Exception as e:
#學生姓名不匹配
print('學生姓名不匹配')
#print(e)
elif number=='4':
try:
student=modifystudentinfo(studentinfo)
except Exception as e:
print('學生姓名不匹配')
#print(e)
elif number=='5':
scorereduce(studentinfo)
elif number=='6':
scorerise(studentinfo)
elif number=='7':
agereduce(studentinfo)
elif number=='8':
agerise(studentinfo)
elif number=='9':
saveinfo(studentinfo)
elif number=='10':
studentinfo=readinfo()
else:
break
input('回主菜單')
main()
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
