網站首頁 編程語言 正文
用Python實現(xiàn)學生信息管理系統(tǒng),供大家參考,具體內容如下
系統(tǒng)功能有:
1.錄入,查找,刪除,修改學生信息
2.學生成績排名
3.顯示全部學生信息
代碼如下:
filename = 'student.txt' #用于存儲學生信息的txt文件名
import os #導入os模塊,用于判斷路徑下文件是否存在
#定義主函數(shù),主函數(shù)的作用是通過選擇不同的選項進入不同的功能
def main():
? ? while True:
? ? ? ? menu()
? ? ? ? choice = int(input('請選擇'))
? ? ? ? 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('謝謝您的使用.')
? ? ? ? ? ? ? ? ? ? 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()
#定義菜單函數(shù),沒什么實際作用的函數(shù),主要是為了讓用戶看明白應該怎么操作
def menu():
? ? print('==========學生信息管理系統(tǒng)==========')
? ? print('-------------功能菜單-------------')
? ? print('1.錄入學生信息')
? ? print('2.查找學生信息')
? ? print('3.刪除學生信息')
? ? print('4.修改學生信息')
? ? print('5.排序')
? ? print('6.統(tǒng)計學生信息')
? ? print('7.顯示所有信息')
? ? print('0.退出')
? ? print('--------------------------------')
#定義學生信息添加函數(shù)
def insert():
? ? student_lst = [] #用于存儲學生信息的列表
? ? while True:
? ? ? ? id = input('請輸入ID(如1001):')
? ? ? ? if not id: break #如果id為空值,就停止
? ? ? ? name = input('請輸入姓名:')
? ? ? ? if not name: break
? ? ? ? try:
? ? ? ? ? ? english = int(input('請輸入英語成績:'))
? ? ? ? ? ? python = int(input('請輸入Python成績:'))
? ? ? ? ? ? java = int(input('請輸入Java成績:'))
? ? ? ? except: #意思是如果上面的成績不是int類型,就會要求重新輸入
? ? ? ? ? ? print('輸入無效,不是整數(shù)類型,請重新輸入.')
? ? ? ? student = {'ID': id, 'Name': name, 'English': english, 'Python': python, 'Java': java} #將新的學生信息生成字典
? ? ? ? student_lst.append(student) #將單條學生信息添加到列表后面
? ? ? ? save(student_lst) #save函數(shù)在后面有定義
? ? ? ? answer = input('是否繼續(xù)添加學生信息?y/n\n')
? ? ? ? if answer == 'y': continue
? ? ? ? else:
? ? ? ? ? ? break
? ? ? ? ? ? print('學生信息錄入完畢.')
#定義學生信息保存函數(shù)
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()
#定義學生信息搜索函數(shù),可以實現(xiàn)按照ID或者姓名搜索
def search():
? ? while True:
? ? ? ? Method = int(input('請輸入查找方法,1表示按ID查找,2表示按姓名查找.'))
? ? ? ? if Method != 1 and Method != 2:
? ? ? ? ? ? print('不是預設的查找方法,請重新輸入.')
? ? ? ? ? ? search() #如果輸入值不是1和2,就會要求重新輸入,可以通過直接再次運行search()函數(shù)實現(xiàn)
? ? ? ? Inf = input('請按照查找方法輸入要查找的學生的信息:') #輸入ID或者姓名
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as sfile: #打開存儲學生信息的文件,實際上在這之前應該有一步判斷文件是否存在,懶得補了,前面套一層if os.path.exists(filename)就行,后面的函數(shù)中有類似的部分
? ? ? ? ? ? stu_ifo = sfile.readlines()
? ? ? ? ? ? d = {}
? ? ? ? ? ? flag = True #用于判斷最后是否找到學生信息
? ? ? ? ? ? if Inf != '':
? ? ? ? ? ? ? ? for item in stu_ifo:
? ? ? ? ? ? ? ? ? ? d = dict(eval(item)) #將文件中的每一行信息轉化為一個字典
? ? ? ? ? ? ? ? ? ? if Method == 1 and d['ID'] == Inf:
? ? ? ? ? ? ? ? ? ? ? ? show_student(d) #show_student函數(shù)在后面有定義,這里也可以之際print(d),但是輸出會很丑,就是直接把一個字典打印出來,不美觀
? ? ? ? ? ? ? ? ? ? ? ? flag = False
? ? ? ? ? ? ? ? ? ? elif Method == 2 and d['Name'] == Inf:
? ? ? ? ? ? ? ? ? ? ? ? show_student(d)
? ? ? ? ? ? ? ? ? ? ? ? flag = False
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('沒有輸入正確的學生信息,請重新輸入.')
? ? ? ? ? ? ? ? search()
? ? ? ? if flag: #flag的作用在這里體現(xiàn)
? ? ? ? ? ? print('未查到學生信息.')
? ? ? ? answer = input('是否繼續(xù)查找學生信息?y/n\n')
? ? ? ? if answer == 'y': continue #continue會重新返回這個函數(shù)
? ? ? ? else: break #break會返回主函數(shù)
#輸出學生信息的一個小函數(shù),作用就是使輸出更美觀
def show_student(lst):
? ? if len(lst) == 0:
? ? ? ? print('沒有查詢到學生信息,無數(shù)據顯示.')
? ? ? ? return
? ? #定義標題顯示格式
? ? format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? print(format_title.format('ID', 'Name', 'English', 'Python', 'Java', 'Sum'))
? ? #定義內容顯示格式
? ? format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? print(format_data.format(lst['ID'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Name'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['English'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Python'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Java'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int(lst['English']) + int(lst['Python']) + int(lst['Java'])))
#定義學生信息刪除函數(shù)
def delete():
? ? while True:
? ? ? ? student_id = input('請輸入要刪除學生的ID:')
? ? ? ? if student_id != '':
? ? ? ? ? ? if os.path.exists(filename): #判斷文件是否存在,如果文件都不存在,那肯定是不能夠刪除的
? ? ? ? ? ? ? ? with open(filename, 'r', encoding = 'utf-8') as stu_file: #打開文件
? ? ? ? ? ? ? ? ? ? student_old = stu_file.readlines()
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? student_old = []
? ? ? ? ? ? flag = False #flag作用與上面的函數(shù)相同
? ? ? ? ? ? if student_old:
? ? ? ? ? ? ? ? with open(filename, 'w', encoding = 'utf-8') as wfile: #上面的stu_file是一個只讀打開方式,這里的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() #show()函數(shù)在下面有定義,意思是展示所有學生信息
? ? ? ? ? ? answer = input('是否繼續(xù)刪除?y/n\n')
? ? ? ? ? ? if answer == 'y': continue
? ? ? ? ? ? else: break
#定義學生信息修改函數(shù)
def modify():
? ? show()
? ? if os.path.exists(filename): #同上
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as rfile: #只讀方式打開
? ? ? ? ? ? student_old = rfile.readlines()
? ? else:
? ? ? ? return #如果沒有學生信息的文件,就回到主函數(shù)
? ? student_id = input('請輸入要修改學生的ID:')
? ? with open(filename, 'w', encoding = 'utf-8') as wfile: #寫入方式打開
? ? ? ? for item in student_old:
? ? ? ? ? ? d = dict(eval(item))
? ? ? ? ? ? flag = False #作用同上
? ? ? ? ? ? if d['ID'] == student_id:
? ? ? ? ? ? ? ? print('找到學生信息,可以修改了.')
? ? ? ? ? ? ? ? try:
? ? ? ? ? ? ? ? ? ? d['Name'] = input('請輸入新的姓名:')
? ? ? ? ? ? ? ? ? ? d['English'] = int(input('請輸入新的English成績:'))
? ? ? ? ? ? ? ? ? ? d['Python'] = int(input('請輸入新的Python成績:'))
? ? ? ? ? ? ? ? ? ? d['Java'] = int(input('請輸入新的Java成績:'))
? ? ? ? ? ? ? ? except:
? ? ? ? ? ? ? ? ? ? print('輸入有誤,請重新輸入.')
? ? ? ? ? ? ? ? wfile.write(str(d) + '\n')
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? wfile.write(str(d) + '\n')
? ? ? ? ? ? ? ? flag = True
? ? ? ? ? ? if flag:
? ? ? ? ? ? ? ? print(f'未找到ID為{student_id}的學生信息.')
? ? ? ? answer = input('是否繼續(xù)修改學生信息?y/n\n')
? ? ? ? if answer == 'y': modify()
#定義學生按成績排序函數(shù)
def sort():
? ? if os.path.exists(filename): #同上
? ? ? ? show()
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as rfile: #只讀方式打開
? ? ? ? ? ? students = rfile.readlines()
? ? ? ? student_new = []
? ? ? ? for item in students:
? ? ? ? ? ? d = dict(eval(item))
? ? ? ? ? ? student_new.append(d)
? ? else:
? ? ? ? return
? ? aord = int(input('請選擇:0表示升序,1表示降序.'))
? ? if aord == 0:
? ? ? ? flag = False
? ? elif aord == 1:
? ? ? ? flag = True
? ? else:
? ? ? ? print('輸入有誤,請重新輸入.')
? ? ? ? sort()
? ? mode = int(input('請選擇排序方式:1英語,2Python,3Java,4Sum'))
? ? if mode == 1: #借助lambda函數(shù)實現(xiàn)排序
? ? ? ? student_new.sort(key = lambda x :int(x['English']), reverse = flag)
? ? elif mode == 2:
? ? ? ? student_new.sort(key = lambda x: int(x['Python']), reverse = flag)
? ? elif mode == 3:
? ? ? ? student_new.sort(key = lambda x: int(x['Java']), reverse = flag)
? ? elif mode == 4:
? ? ? ? student_new.sort(key = lambda x: int(x['English'] + x['Python'] + x['Java']), reverse = flag)
? ? else:
? ? ? ? print('不是預設方法,請重新輸入.')
? ? ? ? sort()
? ? #下面是為了使輸出更加美觀定義的輸出函數(shù)
? ? format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? print(format_title.format('ID', 'Name', 'English', 'Python', 'Java', 'Sum'))
? ? # 定義內容顯示格式
? ? format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? for lst in student_new:
? ? ? ? print(format_data.format(lst['ID'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Name'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['English'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Python'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?lst['Java'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int(lst['English']) + int(lst['Python']) + int(lst['Java'])))
#定義學生數(shù)量統(tǒng)計函數(shù),不多解釋了,比較簡單
def total():
? ? if os.path.exists(filename):
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as rfile:
? ? ? ? ? ? students = rfile.readlines()
? ? ? ? ? ? if students:
? ? ? ? ? ? ? ? print('一共有{}名學生.'.format(len(students)))
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('還沒有錄入學生信息.')
? ? else:
? ? ? ? print('暫未保存數(shù)據信息.')
#定義學生信息展示函數(shù)
def show():
? ? if os.path.exists(filename):
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as rfile:
? ? ? ? ? ? students = rfile.readlines()
? ? ? ? ? ? format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? ? ? ? ? format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? ? ? ? ? print(format_title.format('ID', 'Name', 'English', 'Python', 'Java', 'Sum'))
? ? ? ? ? ? d = {}
? ? ? ? ? ? if len(students) != 0:
? ? ? ? ? ? ? ? for item in students:
? ? ? ? ? ? ? ? ? ? d = dict(eval(item))
? ? ? ? ? ? ? ? ? ? print(format_data.format(d['ID'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?d['Name'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?d['English'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?d['Python'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?d['Java'],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?int(d['English']) + int(d['Python']) + int(d['Java'])))
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('尚未錄入學生信息.')
? ? else:
? ? ? ? print('尚未保存學生信息文件.')
原文鏈接:https://blog.csdn.net/zfk116/article/details/124582224
相關推薦
- 2022-12-29 一文詳細談談GoLang的panic和error_Golang
- 2022-11-27 Qt實現(xiàn)編輯框失去焦點隱藏功能_C 語言
- 2023-07-14 431報錯和解決方法
- 2022-05-21 一次線上mongo慢查詢問題排查處理記錄_MongoDB
- 2022-05-25 Shell腳本獲取jar包pid進行重啟、停止、啟動
- 2022-04-26 python計算寄送包裹重量的實現(xiàn)過程_python
- 2023-03-15 pandas創(chuàng)建DataFrame對象失敗的解決方法_python
- 2022-11-21 Kotlin?Fragment的具體使用詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支