核心代碼
def main():
while True:
menu()
choice=int(input('請選擇:'))
if choice in [0,1,2,3,4,5,6,7]:
if choice==0:
answer=input('您確定要退出系統嗎? y or n')
if answer=='y' or answer=='Y':
print('謝謝您的使用!!!')
break # 退出系統
else:
continue
elif choice==1:
insert() #錄入學生信息
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('----------------學生信息管理系統----------------------')
print('-------------------功能菜單-------------------------')
print('\t\t\t\t 1.錄入學生信息')
print('\t\t\t\t 2.查找學生信息')
print('\t\t\t\t 3.刪除學生信息')
print('\t\t\t\t 4.修改學生信息')
print('\t\t\t\t 5.排序')
print('\t\t\t\t 6.統計學生總人數')
print('\t\t\t\t 7.顯示所有學生信息')
print('\t\t\t\t 0.退出系統')
print('----------------------------------------------------')
運行效果
核心代碼
#錄入學生信息
def insert():
student_list=[]
while True:
id=input('請輸入ID(如1001):')
if not id:
break
name=input('請輸入姓名:')
if not name:
break
try:
math=int(input('請輸入數學成績:'))
english=int(input('請輸入英語成績:'))
chinese=int(input('請輸入語文成績:'))
except:
print('輸入無效,不是整數類型,請重新輸入')
continue
#將錄入的學生信息保存到字典中
student={'id':id,'name':name,'math':math,'english':english,'chinese':chinese}
#將學生信息添加到學生列表中
student_list.append(student)
answer=input('是否繼續添加學生信息?y or n \n')
if answer=='y':
continue
else:
break
#調用save()函數
save(student_list)
print('學生信息錄入完畢!!!')
#保存學生信息
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 delete():
while True:
student_id=input('請輸入需要刪除的學生的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 #標記是否刪除
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={} #定義空字典
for item in student_old:
d=dict(eval(item)) #將字符串轉成字典
if d['id'] != student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'ID為{student_id}的學生信息已被刪除!')
else:
print(f'沒有找到ID為{student_id}的學生信息!')
else:
print('系統內無學生信息!')
break
show() #刪除信息后展示系統內學生信息
answer=input('是否繼續刪除? y or n \n')
if 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('請輸入要修改的學生的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('找到學生信息,可以進行修改該學生的相關信息了!')
while True:
try:
d['name']=input('請輸入姓名:')
d['math'] = input('請輸入數學成績:')
d['english'] = input('請輸入英語成績:')
d['chinese'] = input('請輸入語文成績:')
except:
print('您的輸入有誤,請重新輸入!')
else:
break
wfile.write(str(d) + '\n')
print('修改成功!!!')
else:
wfile.write(str(d) + '\n')
answer=input('是否需要繼續修改其他學生信息? y or n \n')
if answer=='y':
modify()
運行效果
核心代碼
#查找學生信息
def search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mode=input('按ID查找請輸入1,按姓名查找請輸入2:')
if mode=='1':
id=input('請輸入學生ID:')
elif mode=='2':
name=input('請輸入學生姓名:')
else:
print('您的輸入有誤,請重新輸入!')
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)
#顯示查詢結果
show_student(student_query)
#清空列表
student_query.clear()
answer=input('是否要繼續查詢? y or n \n')
if answer=='y':
continue
else:
break
else:
print('暫未保存學生信息!')
return
def show_student(lst):
if len(lst)==0:
print('沒有查詢到學生信息,無數據顯示!!!')
return
#定義標題顯示格式
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^6}\t{:^6}'
print(format_title.format('ID','姓名','數學成績','英語成績','語文成績','總成績'))
#定義內容顯示格式
format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^14}\t{:^8}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('math'),
item.get('english'),
item.get('chinese'),
int(item.get('math'))+int(item.get('english'))+int(item.get('chinese'))))
運行效果
核心代碼
#統計學生總人數
def total():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
if students:
print(f'一共有{len(students)}名學生!')
else:
print('還沒有錄入學生信息!')
else:
print('暫未保存數據信息.....')
運行效果
核心代碼
#展示所有學生信息
def show():
student_lst=[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
for item in students:
student_lst.append(eval(item))
if student_lst:
show_student(student_lst)
else:
print('暫未保存過數據!!!!')
運行效果
核心代碼
#排序
def sort():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
student_list=rfile.readlines()
student_new=[]
for item in student_list:
d=dict(eval(item))
student_new.append(d)
else:
return
asc_or_desc=input('請選擇排序方式,1.升序 2.降序 :')
if asc_or_desc=='1':
asc_or_desc_bool=False
elif asc_or_desc=='2':
asc_or_desc_bool=True
else:
print('您的輸入有誤,請重新輸入 :')
sort()
mode=input('請選擇排序方式,1.按數學成績排序 2.按英語成績排序 3.按語文成績排序 4.按總成績排序 :')
if mode=='1':
student_new.sort(key=lambda x:int(x['math']),reverse=asc_or_desc_bool)
elif mode=='2':
student_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)
elif mode=='3':
student_new.sort(key=lambda x:int(x['chinese']),reverse=asc_or_desc_bool)
elif mode=='4':
student_new.sort(key=lambda x:int(x['math'])+int(x['english'])+int(x['chinese']),reverse=asc_or_desc_bool)
else:
print('您的輸入有誤,請重新輸入:')
sort()
show_student(student_new)
運行效果
點擊下載
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
本文標題:Python課程設計之學生信息管理系統-創新互聯
當前網址:http://newbst.com/article6/dipgig.html
成都網站建設公司_創新互聯,為您提供網站內鏈、手機網站建設、網站導航、品牌網站建設、定制開發、關鍵詞優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯