學(xué)生成績(jī)管理系統(tǒng)主要包括錄入學(xué)生信息、查找學(xué)生信息、刪除學(xué)生信息、修改學(xué)生信息、排序、統(tǒng)計(jì)學(xué)生總?cè)藬?shù)、顯示學(xué)生信息和退出系統(tǒng)。
系統(tǒng)界面編寫(菜單顯示函數(shù)):
def menu():
print('============學(xué)生信息管理系統(tǒng)============')
print('---------------功能菜單---------------')
print('\t\t\t1.錄入學(xué)生信息')
print('\t\t\t2.查找學(xué)生信息')
print('\t\t\t3.刪除學(xué)生信息')
print('\t\t\t4.修改學(xué)生信息')
print('\t\t\t5.排序')
print('\t\t\t6.統(tǒng)計(jì)學(xué)生總?cè)藬?shù)')
print('\t\t\t7.顯示所有學(xué)生信息')
print('\t\t\t0.退出系統(tǒng)')
print('-------------------------------------')
main函數(shù):?
def main():
while(True):
menu()
choice = int(input('請(qǐng)選擇功能序號(hào):'))
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('您確定要退出系統(tǒng)嗎?(Y/N)')
if answer == 'y' or answer == 'Y':
print('退出系統(tǒng),謝謝使用!')
break
else:
continue
elif choice == 1:
insert() #錄入學(xué)生信息
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
錄入學(xué)生信息函數(shù):
def insert():
stu_list=[]
while True:
id=input('請(qǐng)輸入學(xué)生ID(如1001):')
if not id:
break
name=input('請(qǐng)輸入學(xué)生姓名:')
if not name:
break
try:
english=int(input('請(qǐng)輸入英語(yǔ)成績(jī):'))
java=int(input('請(qǐng)輸入java成績(jī):'))
python=int(input('請(qǐng)輸入python成績(jī):'))
except:
print('輸入無(wú)效,不是整數(shù),請(qǐng)重新輸入:')
continue
#將錄入的學(xué)生成績(jī)保存到字典中
student={'id':id,'name':name,'English':english,'Java':java,'Python':python}
#將學(xué)生信息添加到列表中
stu_list.append(student)
answer=input('是否繼續(xù)添加(Y/N):')
if answer == 'y' or answer == 'Y':
continue
else:
break
#調(diào)用save()函數(shù)保存在文件中
save(stu_list)
print('學(xué)生信息錄入完畢')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt=open(filename,'w',encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
查找學(xué)生信息函數(shù):
def search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mod=input('若根據(jù)ID查找學(xué)生信息,請(qǐng)輸入1;若根據(jù)姓名查找學(xué)生信息,請(qǐng)輸入2:')
if mod=='1':
id=input('請(qǐng)輸入學(xué)生ID:')
elif mod=='2':
name=input('請(qǐng)輸入學(xué)生姓名:')
else:
print('輸入有誤。請(qǐng)重新輸入!')
search()
with open(filename,'r',encoding='utf-8')as rfile:
student=rfile.readlines()
for item in student:
d=dict(eval(item))
if id!='':
if d['id']==id:
student_query.append(d)
elif name!='':
if d['name']==name:
student_query.append(d)
#顯示查詢結(jié)果
show_student(student_query)
#清空列表
student_query.clear()
answer=input('是否繼續(xù)查詢?(Y/N):\n')
if answer=='y' or answer=='Y':
continue
else:
break
else:
print('未保存學(xué)員信息!')
return
def show_student(lst):
if len(lst)==0:
print('未查詢到該學(xué)生信息!!!')
return
# 定義標(biāo)題的顯示格式
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'
print(format_title.format('ID','Name','English','Java','Python','Grade'))
# 定義內(nèi)容的顯示格式
format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('English'),
item.get('Java'),
item.get('Python'),
int(item.get('English'))+int(item.get('Java'))+int(item.get('Python'))
))
刪除學(xué)生信息函數(shù):
def delete():
while(True):
student_id=input("請(qǐng)輸入要?jiǎng)h除學(xué)生的ID:")
if student_id!='':
if os.path.exists(filename): #判斷文件是否存在
with open(filename,'r',encoding='utf-8') as file: #存在的話打開
student_old=file.readlines()
else:
student_old=[] #不存在
flag=False #標(biāo)記是否刪除
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={}
for item in student_old:
d=dict(eval(item)) #將字符串轉(zhuǎn)成字典
if d['id']!=student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id為{student_id}的學(xué)生信息已被刪除!')
else:
print(f'未查詢到id為{student_id}的學(xué)生信息。')
else:
print('無(wú)學(xué)生信息')
break
show()
answer=input('是否繼續(xù)刪除?(Y/N):')
if answer=='Y'or answer=='y':
continue
else:
break
?修改學(xué)生信息函數(shù):
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
return
student_id=input('請(qǐng)輸入要修改學(xué)生id:')
with open(filename,'w',encoding='utf-8') as wfile:
for item in student_old:
d=dict(eval(item))
if d['id']==student_id:
print('找到該學(xué)生,可以修改相關(guān)信息!')
while True:
try:
d['name']=input('請(qǐng)輸入學(xué)生姓名:')
d['English']=input('請(qǐng)輸入英語(yǔ)成績(jī):')
d['Java']=input('請(qǐng)輸入java成績(jī):')
d['Python']=input('請(qǐng)輸入python成績(jī):')
except:
print('輸入有誤,請(qǐng)重新輸入')
else:
break
wfile.write(str(d)+'\n')
print('修改成功!')
else:
wfile.write(str(d) + '\n')
answer=input('是否繼續(xù)修改?(Y/N):')
if answer=='y' or answer =='Y':
modify()
?排序函數(shù):
def sort():
show()
student_list=[]
asc_or_desc=input('請(qǐng)選擇排序方式(0為升序,1為降序):')
mode=input('請(qǐng)選擇排序依據(jù)(1為按英語(yǔ)成績(jī)排序,2為按Java成績(jī)排序,3為按Python成績(jī)排序,0為按總成績(jī)排序):')
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
students=rfile.readlines()
for item in students:
d=dict(eval(item))
student_list.append(d)
else:
print('暫未保存學(xué)生信息!')
return
if asc_or_desc=='0':
asc_or_desc_bool=False
elif asc_or_desc=='1':
asc_or_desc_bool=True
else:
print('輸入有誤,請(qǐng)重新輸入:')
sort()
if mode=='1':
student_list.sort(key=lambda x:int(x['English']),reverse=asc_or_desc_bool) #使用匿名函數(shù)lambda
elif mode=='2':
student_list.sort(key=lambda x:int(x['Java']), reverse=asc_or_desc_bool)
elif mode=='3':
student_list.sort(key=lambda x:int(x['Python']), reverse=asc_or_desc_bool)
elif mode=='0':
student_list.sort(key=lambda x:int(x['English'])+int(x['Java'])+int(x['Python']), reverse=asc_or_desc_bool)
else:
print('輸入有誤,請(qǐng)重新輸入!')
sort()
show_student(student_list)
統(tǒng)計(jì)學(xué)生總?cè)藬?shù)函數(shù):
def total():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
students=rfile.readlines()
if students==[]:
print('暫未錄入學(xué)生信息,請(qǐng)先錄入!')
return
else:
print(f'共有{len(students)}名學(xué)生!')
else:
print('暫未保存學(xué)生信息!!!')
return
顯示學(xué)生信息函數(shù):
def show():
student_list=[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
student_l=rfile.readlines()
for item in student_l:
student_list.append(eval(item)) #eval還原為字典類型
if student_list:
show_student(student_list)
else:
print('暫未保存學(xué)生信息!')
return
執(zhí)行:
if __name__ == '__main__': # 只有執(zhí)行該程序時(shí),才會(huì)執(zhí)行 import 是不可以執(zhí)行的
main()
完整源代碼:
filename='student.txt'
import os
def main():
while(True):
menu()
choice = int(input('請(qǐng)選擇功能序號(hào):'))
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('您確定要退出系統(tǒng)嗎?(Y/N)')
if answer == 'y' or answer == 'Y':
print('退出系統(tǒng),謝謝使用!')
break
else:
continue
elif choice == 1:
insert() #錄入學(xué)生信息
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
def menu():
print('============學(xué)生信息管理系統(tǒng)============')
print('---------------功能菜單---------------')
print('\t\t\t1.錄入學(xué)生信息')
print('\t\t\t2.查找學(xué)生信息')
print('\t\t\t3.刪除學(xué)生信息')
print('\t\t\t4.修改學(xué)生信息')
print('\t\t\t5.排序')
print('\t\t\t6.統(tǒng)計(jì)學(xué)生總?cè)藬?shù)')
print('\t\t\t7.顯示所有學(xué)生信息')
print('\t\t\t0.退出系統(tǒng)')
print('-------------------------------------')
def insert():
stu_list=[]
while True:
id=input('請(qǐng)輸入學(xué)生ID(如1001):')
if not id:
break
name=input('請(qǐng)輸入學(xué)生姓名:')
if not name:
break
try:
english=int(input('請(qǐng)輸入英語(yǔ)成績(jī):'))
java=int(input('請(qǐng)輸入java成績(jī):'))
python=int(input('請(qǐng)輸入python成績(jī):'))
except:
print('輸入無(wú)效,不是整數(shù),請(qǐng)重新輸入:')
continue
#將錄入的學(xué)生成績(jī)保存到字典中
student={'id':id,'name':name,'English':english,'Java':java,'Python':python}
#將學(xué)生信息添加到列表中
stu_list.append(student)
answer=input('是否繼續(xù)添加(Y/N):')
if answer == 'y' or answer == 'Y':
continue
else:
break
#調(diào)用save()函數(shù)保存在文件中
save(stu_list)
print('學(xué)生信息錄入完畢')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt=open(filename,'w',encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
def search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mod=input('若根據(jù)ID查找學(xué)生信息,請(qǐng)輸入1;若根據(jù)姓名查找學(xué)生信息,請(qǐng)輸入2:')
if mod=='1':
id=input('請(qǐng)輸入學(xué)生ID:')
elif mod=='2':
name=input('請(qǐng)輸入學(xué)生姓名:')
else:
print('輸入有誤。請(qǐng)重新輸入!')
search()
with open(filename,'r',encoding='utf-8')as rfile:
student=rfile.readlines()
for item in student:
d=dict(eval(item))
if id!='':
if d['id']==id:
student_query.append(d)
elif name!='':
if d['name']==name:
student_query.append(d)
#顯示查詢結(jié)果
show_student(student_query)
#清空列表
student_query.clear()
answer=input('是否繼續(xù)查詢?(Y/N):\n')
if answer=='y' or answer=='Y':
continue
else:
break
else:
print('未保存學(xué)員信息!')
return
def show_student(lst):
if len(lst)==0:
print('未查詢到該學(xué)生信息!!!')
return
# 定義標(biāo)題的顯示格式
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'
print(format_title.format('ID','Name','English','Java','Python','Grade'))
# 定義內(nèi)容的顯示格式
format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('English'),
item.get('Java'),
item.get('Python'),
int(item.get('English'))+int(item.get('Java'))+int(item.get('Python'))
))
def delete():
while(True):
student_id=input("請(qǐng)輸入要?jiǎng)h除學(xué)生的ID:")
if student_id!='':
if os.path.exists(filename): #判斷文件是否存在
with open(filename,'r',encoding='utf-8') as file: #存在的話打開
student_old=file.readlines()
else:
student_old=[] #不存在
flag=False #標(biāo)記是否刪除
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={}
for item in student_old:
d=dict(eval(item)) #將字符串轉(zhuǎn)成字典
if d['id']!=student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id為{student_id}的學(xué)生信息已被刪除!')
else:
print(f'未查詢到id為{student_id}的學(xué)生信息。')
else:
print('無(wú)學(xué)生信息')
break
show()
answer=input('是否繼續(xù)刪除?(Y/N):')
if answer=='Y'or answer=='y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as rfile:
student_old = rfile.readlines()
else:
return
student_id=input('請(qǐng)輸入要修改學(xué)生id:')
with open(filename,'w',encoding='utf-8') as wfile:
for item in student_old:
d=dict(eval(item))
if d['id']==student_id:
print('找到該學(xué)生,可以修改相關(guān)信息!')
while True:
try:
d['name']=input('請(qǐng)輸入學(xué)生姓名:')
d['English']=input('請(qǐng)輸入英語(yǔ)成績(jī):')
d['Java']=input('請(qǐng)輸入java成績(jī):')
d['Python']=input('請(qǐng)輸入python成績(jī):')
except:
print('輸入有誤,請(qǐng)重新輸入')
else:
break
wfile.write(str(d)+'\n')
print('修改成功!')
else:
wfile.write(str(d) + '\n')
answer=input('是否繼續(xù)修改?(Y/N):')
if answer=='y' or answer =='Y':
modify()
def sort():
show()
student_list=[]
asc_or_desc=input('請(qǐng)選擇排序方式(0為升序,1為降序):')
mode=input('請(qǐng)選擇排序依據(jù)(1為按英語(yǔ)成績(jī)排序,2為按Java成績(jī)排序,3為按Python成績(jī)排序,0為按總成績(jī)排序):')
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
students=rfile.readlines()
for item in students:
d=dict(eval(item))
student_list.append(d)
else:
print('暫未保存學(xué)生信息!')
return
if asc_or_desc=='0':
asc_or_desc_bool=False
elif asc_or_desc=='1':
asc_or_desc_bool=True
else:
print('輸入有誤,請(qǐng)重新輸入:')
sort()
if mode=='1':
student_list.sort(key=lambda x:int(x['English']),reverse=asc_or_desc_bool) #使用匿名函數(shù)lambda
elif mode=='2':
student_list.sort(key=lambda x:int(x['Java']), reverse=asc_or_desc_bool)
elif mode=='3':
student_list.sort(key=lambda x:int(x['Python']), reverse=asc_or_desc_bool)
elif mode=='0':
student_list.sort(key=lambda x:int(x['English'])+int(x['Java'])+int(x['Python']), reverse=asc_or_desc_bool)
else:
print('輸入有誤,請(qǐng)重新輸入!')
sort()
show_student(student_list)
def total():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
students=rfile.readlines()
if students==[]:
print('暫未錄入學(xué)生信息,請(qǐng)先錄入!')
return
else:
print(f'共有{len(students)}名學(xué)生!')
else:
print('暫未保存學(xué)生信息!!!')
return
def show():
student_list=[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as rfile:
student_l=rfile.readlines()
for item in student_l:
student_list.append(eval(item)) #eval還原為字典類型
if student_list:
show_student(student_list)
else:
print('暫未保存學(xué)生信息!')
return
if __name__ == '__main__': # 只有執(zhí)行該程序時(shí),才會(huì)執(zhí)行 import 是不可以執(zhí)行的
main()
運(yùn)行結(jié)果:
錄入信息截圖:
?
查詢信息截圖:
統(tǒng)計(jì)學(xué)生總?cè)藬?shù)截圖:
?
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-503543.html
?排序截圖:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-503543.html
?
到了這里,關(guān)于Python入門——學(xué)生成績(jī)管理系統(tǒng)(錄入、查找、刪除、修改、排序、統(tǒng)計(jì)、顯示)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!