欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)

 更新時(shí)間:2022年07月28日 09:27:24   作者:菜康勇  
這篇文章主要為大家詳細(xì)介紹了用python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用Python實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),供大家參考,具體內(nèi)容如下

系統(tǒng)功能有:

1.錄入,查找,刪除,修改學(xué)生信息
2.學(xué)生成績排名
3.顯示全部學(xué)生信息

代碼如下:

filename = 'student.txt' #用于存儲學(xué)生信息的txt文件名
import os #導(dǎo)入os模塊,用于判斷路徑下文件是否存在
#定義主函數(shù),主函數(shù)的作用是通過選擇不同的選項(xiàng)進(jìn)入不同的功能
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í)際作用的函數(shù),主要是為了讓用戶看明白應(yīng)該怎么操作
def menu():
? ? print('==========學(xué)生信息管理系統(tǒng)==========')
? ? print('-------------功能菜單-------------')
? ? print('1.錄入學(xué)生信息')
? ? print('2.查找學(xué)生信息')
? ? print('3.刪除學(xué)生信息')
? ? print('4.修改學(xué)生信息')
? ? print('5.排序')
? ? print('6.統(tǒng)計(jì)學(xué)生信息')
? ? print('7.顯示所有信息')
? ? print('0.退出')
? ? print('--------------------------------')
#定義學(xué)生信息添加函數(shù)
def insert():
? ? student_lst = [] #用于存儲學(xué)生信息的列表
? ? 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類型,就會(huì)要求重新輸入
? ? ? ? ? ? print('輸入無效,不是整數(shù)類型,請重新輸入.')
? ? ? ? student = {'ID': id, 'Name': name, 'English': english, 'Python': python, 'Java': java} #將新的學(xué)生信息生成字典
? ? ? ? student_lst.append(student) #將單條學(xué)生信息添加到列表后面
? ? ? ? save(student_lst) #save函數(shù)在后面有定義
? ? ? ? answer = input('是否繼續(xù)添加學(xué)生信息?y/n\n')
? ? ? ? if answer == 'y': continue
? ? ? ? else:
? ? ? ? ? ? break
? ? ? ? ? ? print('學(xué)生信息錄入完畢.')
#定義學(xué)生信息保存函數(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()
#定義學(xué)生信息搜索函數(shù),可以實(shí)現(xiàn)按照ID或者姓名搜索
def search():
? ? while True:
? ? ? ? Method = int(input('請輸入查找方法,1表示按ID查找,2表示按姓名查找.'))
? ? ? ? if Method != 1 and Method != 2:
? ? ? ? ? ? print('不是預(yù)設(shè)的查找方法,請重新輸入.')
? ? ? ? ? ? search() #如果輸入值不是1和2,就會(huì)要求重新輸入,可以通過直接再次運(yùn)行search()函數(shù)實(shí)現(xiàn)
? ? ? ? Inf = input('請按照查找方法輸入要查找的學(xué)生的信息:') #輸入ID或者姓名
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as sfile: #打開存儲學(xué)生信息的文件,實(shí)際上在這之前應(yīng)該有一步判斷文件是否存在,懶得補(bǔ)了,前面套一層if os.path.exists(filename)就行,后面的函數(shù)中有類似的部分
? ? ? ? ? ? stu_ifo = sfile.readlines()
? ? ? ? ? ? d = {}
? ? ? ? ? ? flag = True #用于判斷最后是否找到學(xué)生信息
? ? ? ? ? ? if Inf != '':
? ? ? ? ? ? ? ? for item in stu_ifo:
? ? ? ? ? ? ? ? ? ? d = dict(eval(item)) #將文件中的每一行信息轉(zhuǎn)化為一個(gè)字典
? ? ? ? ? ? ? ? ? ? if Method == 1 and d['ID'] == Inf:
? ? ? ? ? ? ? ? ? ? ? ? show_student(d) #show_student函數(shù)在后面有定義,這里也可以之際print(d),但是輸出會(huì)很丑,就是直接把一個(gè)字典打印出來,不美觀
? ? ? ? ? ? ? ? ? ? ? ? flag = False
? ? ? ? ? ? ? ? ? ? elif Method == 2 and d['Name'] == Inf:
? ? ? ? ? ? ? ? ? ? ? ? show_student(d)
? ? ? ? ? ? ? ? ? ? ? ? flag = False
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('沒有輸入正確的學(xué)生信息,請重新輸入.')
? ? ? ? ? ? ? ? search()
? ? ? ? if flag: #flag的作用在這里體現(xiàn)
? ? ? ? ? ? print('未查到學(xué)生信息.')
? ? ? ? answer = input('是否繼續(xù)查找學(xué)生信息?y/n\n')
? ? ? ? if answer == 'y': continue #continue會(huì)重新返回這個(gè)函數(shù)
? ? ? ? else: break #break會(huì)返回主函數(shù)
#輸出學(xué)生信息的一個(gè)小函數(shù),作用就是使輸出更美觀
def show_student(lst):
? ? if len(lst) == 0:
? ? ? ? print('沒有查詢到學(xué)生信息,無數(shù)據(jù)顯示.')
? ? ? ? return
? ? #定義標(biāo)題顯示格式
? ? format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
? ? print(format_title.format('ID', 'Name', 'English', 'Python', 'Java', 'Sum'))
? ? #定義內(nèi)容顯示格式
? ? 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'])))
#定義學(xué)生信息刪除函數(shù)
def delete():
? ? while True:
? ? ? ? student_id = input('請輸入要?jiǎng)h除學(xué)生的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是一個(gè)只讀打開方式,這里的wfile是可以寫入的打開方式,目的是存儲刪除后的學(xué)生信息
? ? ? ? ? ? ? ? ? ? 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}的學(xué)生信息已刪除.')
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? print(f'沒有找到ID為{student_id}的學(xué)生.')
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('沒有學(xué)生信息.')
? ? ? ? ? ? ? ? break
? ? ? ? ? ? show() #show()函數(shù)在下面有定義,意思是展示所有學(xué)生信息
? ? ? ? ? ? answer = input('是否繼續(xù)刪除?y/n\n')
? ? ? ? ? ? if 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 #如果沒有學(xué)生信息的文件,就回到主函數(shù)
? ? student_id = input('請輸入要修改學(xué)生的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('找到學(xué)生信息,可以修改了.')
? ? ? ? ? ? ? ? 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}的學(xué)生信息.')
? ? ? ? answer = input('是否繼續(xù)修改學(xué)生信息?y/n\n')
? ? ? ? if answer == 'y': modify()
#定義學(xué)生按成績排序函數(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ù)實(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('不是預(yù)設(shè)方法,請重新輸入.')
? ? ? ? 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'))
? ? # 定義內(nèi)容顯示格式
? ? 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'])))
#定義學(xué)生數(shù)量統(tǒng)計(jì)函數(shù),不多解釋了,比較簡單
def total():
? ? if os.path.exists(filename):
? ? ? ? with open(filename, 'r', encoding = 'utf-8') as rfile:
? ? ? ? ? ? students = rfile.readlines()
? ? ? ? ? ? if students:
? ? ? ? ? ? ? ? print('一共有{}名學(xué)生.'.format(len(students)))
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print('還沒有錄入學(xué)生信息.')
? ? else:
? ? ? ? print('暫未保存數(shù)據(jù)信息.')
#定義學(xué)生信息展示函數(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('尚未錄入學(xué)生信息.')
? ? else:
? ? ? ? print('尚未保存學(xué)生信息文件.')

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論