av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)

瀏覽:18日期:2022-06-23 09:07:44
構(gòu)思

學(xué)生管理系統(tǒng) 應(yīng)該包含老師注冊登錄 管理學(xué)生信息(增刪改查)還有數(shù)據(jù)持久化

因?yàn)閿?shù)據(jù)存入JSON文件 增刪改查都需要讀取和修改文件所以需要一個(gè)讀寫文件的方法文件 file_manager密碼加密可以用到哈希模塊文件 tools老師和學(xué)生的類文件 model程序入口(主頁)文件index核心增刪改查文件 student_manager

file_manager.py

'''Project: ClassStudentCreator: 貓貓Create time: 2021-02-25 20:23IDE: PyCharmIntroduction:https://blog.csdn.net/Cantevenl/article/details/115439530'''base_dir = ’files/’ # 定義一個(gè)變量 文件路徑# 讀文件的函數(shù)def read_file(file_name): try:with open(base_dir + file_name, ’r’, encoding=’utf8’) as file: content = file.read() return content except FileNotFoundError:print(’文件未找到’)def write_json(file_name, data): with open(base_dir + file_name, ’w’, encoding=’utf8’) as file:import jsonjson.dump(data, file)def read_json(file_name, default_data): try:with open(base_dir + file_name, ’r’, encoding=’utf8’) as file: import json return json.load(file) except FileNotFoundError:# print(’文件未找到’)return default_data

tools.py

'''Project: ClassStudentCreator: 貓貓Create time: 2021-02-25 20:24IDE: PyCharmIntroduction:https://blog.csdn.net/Cantevenl/article/details/115439530'''# 加密密碼import hashlibdef encrypt_password(passwd, x=’zhumaogouniu’): h = hashlib.sha256() h.update(passwd.encode(’utf8’)) h.update(x.encode(’utf8’)) return h.hexdigest()

model.py

'''Project: ClassStudentCreator: 貓貓Create time: 2021-02-25 20:24IDE: PyCharmIntroduction:https://blog.csdn.net/Cantevenl/article/details/115439530'''# 創(chuàng)建一個(gè)老師對象class Teacher(object): def __init__(self, name, password):import toolsself.name = nameself.password = tools.encrypt_password(password)class Student(object): def __init__(self, s_id, name, age, gender, tel):self.s_id = s_idself.name = nameself.age = ageself.gender = genderself.tel = tel

index.py

'''Project: ClassStudentCreator: 貓貓Create time: 2021-02-25 20:25IDE: PyCharmIntroduction:https://blog.csdn.net/Cantevenl/article/details/115439530'''import sysimport file_manager # 導(dǎo)入自己寫的讀取文件模塊import modelimport student_managerdef register(): # 讀取文件,查看文件里是否有數(shù)據(jù)。如果文件不存在,默認(rèn)是一個(gè)字典 data = file_manager.read_json(’data.json’, {}) while True:teacher_name = input(’請輸入賬號(hào)(3~6位):’)if not 2 <= len(teacher_name) <= 6: print(’賬號(hào)不符合要求,請重新輸入!’)else: break if teacher_name in data:print(’注冊失敗!該賬號(hào)已經(jīng)被注冊!’)return while True:password = input(’請輸入密碼(6~12位):’)if not 6 <= len(password) <= 12: print(’密碼不符合要求,請重新輸入!’)else: break # 用戶名密碼都已經(jīng)輸入正確以后 將用戶名和密碼以key:value形式儲(chǔ)存 # 可以創(chuàng)建一個(gè)teacher對象 t = model.Teacher(teacher_name, password) data[t.name] = t.password file_manager.write_json(’data.json’, data)def login(): # 讀取文件,查看文件里是否有數(shù)據(jù)。如果文件不存在,默認(rèn)是一個(gè)字典 data = file_manager.read_json(’data.json’, {}) teacher_name = input(’請輸入老師賬號(hào):’) if teacher_name not in data:print(’登錄失敗!該賬號(hào)沒有注冊!’)return password = input(’請輸入密碼:’) import tools if data[teacher_name] == tools.encrypt_password(password):student_manager.name = teacher_nameprint(’登錄成功’)student_manager.show_manager() else:print(’密碼錯(cuò)誤,登陸失敗!’)def start(): content = file_manager.read_file(’welcome.txt’) while True:operate = input(content + ’n請選擇(1-3):’)if operate == ’1’: print(’登錄’) login()elif operate == ’2’: print(’注冊’) register()elif operate == ’3’: print(’退出’) # break # 把循環(huán)退出 # exit(0) # 退出整個(gè)程序 sys.exit(0) # 退出程序else: print(’輸入有誤!’)if __name__ == ’__main__’: start()

student_manager.py

'''Project: ClassStudentCreator: 貓貓Create time: 2021-02-25 20:26IDE: PyCharmIntroduction:https://blog.csdn.net/Cantevenl/article/details/115439530'''import file_managerimport modelname = ’’# 添加def add_student(): x = file_manager.read_json(name + ’.json’, {}) if not x:students = []num = 0 else:students = x[’all_student’]# s_id通過json字典里的num來生成num = int(x[’num’]) while True:s_name = input(’請輸入學(xué)生姓名:’)s_age = input(’請輸入年齡:’)s_gender = input(’請輸入性別:’)s_tel = input(’請輸入電話號(hào)碼:’)num += 1# 字符串的zfill方法,在字符串的前面補(bǔ)0s_id = ’student_id_’ + str(num).zfill(3)# 創(chuàng)建一個(gè)Student對象s = model.Student(s_id, s_name, s_age, s_gender, s_tel)students.append(s.__dict__)# 拼接字典data = {’all_student’: students, ’num’: len(students)}# 把數(shù)據(jù)寫入到文件里 以老師名字.json 把data寫進(jìn)去file_manager.write_json(name + ’.json’, data)choice = input(’添加成功!n1.繼續(xù)n2.返回n請選擇(1-2):’)if choice == ’2’: break# 顯示 查找def show_student(): choice = input(’1.查看所有學(xué)生n2.根據(jù)姓名查找n3.根據(jù)學(xué)號(hào)查找n其他:返回n請選擇:’) students = file_manager.read_json(name + ’.json’, {})all_students = students.get(’all_student’, []) # get拿列表里的數(shù)據(jù),拿不到默認(rèn)是一個(gè)空列表[] if not all_students:print(’該老師沒有學(xué)生,請?zhí)砑訉W(xué)生’)return if choice == ’1’: # 查看所有學(xué)生pass elif choice == ’2’: # 根據(jù)姓名查看s_name = input(’請輸入學(xué)生的姓名:’)all_students = filter(lambda s: s[’name’] == s_name, all_students) # students就只保留了指定名字的學(xué)生 elif choice == ’3’: # 學(xué)號(hào)查找s_id = input(’請輸入學(xué)生的學(xué)號(hào):’)all_students = filter(lambda s: s[’s_id’] == s_id, all_students) else:return if not students:print(’未找到學(xué)生’)return for student in all_students:print(’學(xué)號(hào):{s_id},姓名:{name},性別:{gender},年齡{age},電話號(hào)碼{tel}’.format(**student))# 修改def modify_student(): key = value = ’’ m = file_manager.read_json(name + ’.json’, {}) all_students = m.get(’all_student’, []) if not all_students:print(’該老師沒有學(xué)生,請?zhí)砑訉W(xué)生’)return modify = input(’1.根據(jù)姓名修改n2.根據(jù)學(xué)號(hào)修改n其他:返回n請選擇:’) if modify == ’1’:value = input(’請輸入要修改的學(xué)生名字:’)key = ’name’ elif modify == ’2’:value = input(’請輸入要修改的學(xué)生學(xué)號(hào):’)key = ’s_id’ else:return students = list(filter(lambda s: s[key] == value, all_students)) if not all_students:print(’沒有找到對應(yīng)的學(xué)生’)return for i, student in enumerate(students):print(’{xiabiao} 學(xué)號(hào):{s_id},姓名:{name},性別:{gender},年齡{age},電話號(hào)碼{tel}’.format(xiabiao=i, **student)) n = int(input(’請輸入需要修改的學(xué)生的標(biāo)號(hào)(0~{}),q-->返回:’.format(len(students) - 1))) if not 0 <= n <= len(students):print(’輸入的內(nèi)容錯(cuò)誤’)return all_students.remove(students[n]) students[n][’s_id’] = new_input((students[n][’s_id’]), '請輸入修改后的學(xué)生學(xué)號(hào)[回車則不修改]:') students[n][’name’] = new_input((students[n][’name’]), '請輸入修改后的學(xué)生姓名[回車則不修改]:') students[n][’gender’] = new_input((students[n][’gender’]), '請輸入修改后的學(xué)生性別[回車則不修改]:') students[n][’age’] = new_input((students[n][’age’]), '請輸入修改后的學(xué)生年齡[回車則不修改]:') students[n][’tel’] = new_input((students[n][’tel’]), '請輸入修改后的學(xué)生電話號(hào)碼[回車則不修改]:') all_students.append(students[n]) print(all_students) m[’all_student’] = all_students file_manager.write_json(name + ’.json’, m)# 刪除def delete_student(): y = file_manager.read_json(name + ’.json’, {}) all_students = y.get(’all_student’, []) key = value = ’’ if not all_students:print(’該老師沒有學(xué)生,請?zhí)砑訉W(xué)生’)return num = input(’1.按照姓名刪除n2.按照學(xué)號(hào)刪除n其他:返回n請選擇:’) if num == ’1’:key = ’name’value = input(’請輸入要?jiǎng)h除的學(xué)生名字:’) elif num == ’2’:key = ’s_id’value = input(’請輸入要?jiǎng)h除的學(xué)生學(xué)號(hào):’) else:return students = list(filter(lambda s: s.get(key, ’’) == value, all_students)) if not students:print(’沒有找到對應(yīng)的學(xué)生’)return for i, student in enumerate(students):print(’{xiabiao} 學(xué)號(hào):{s_id},姓名:{name},性別:{gender},年齡{age},電話號(hào)碼{tel}’.format(xiabiao=i, **student)) n = input(’請輸入需要?jiǎng)h除的學(xué)生的標(biāo)號(hào)(0~{}),q-->返回:’.format(len(students) - 1)) if not n.isdigit() or not 0 <= int(n) <= len(students):print(’輸入的內(nèi)容錯(cuò)誤’)return # 將學(xué)生從all_students里刪除 # all_students.pop(n) all_students.remove(students[int(n)]) # 更新新的名單 y[’all_student’] = all_students file_manager.write_json(name + ’.json’, y)# 顯示管理頁面def show_manager(): content = file_manager.read_file(’students_page.txt’) % name while True:print(content)operate = input(’請選擇(1-5):’)if operate == ’1’: add_student()elif operate == ’2’: show_student()elif operate == ’3’: modify_student()elif operate == ’4’: delete_student()elif operate == ’5’: breakelse: print(’輸入錯(cuò)誤!’)# 修改時(shí)用的input方法def new_input(old, new): input_str = input(new) if len(input_str) > 0:return input_str else:return old

美化文本

通過自己寫的讀取文件方法可以直接讀取文本(類似界面)

welcome.txt

================================ ** 歡迎來到學(xué)生管理系統(tǒng)** 1. 登 錄 2. 注 冊 3. 退 出 ** **================================

students_page.txt

================================❀❀ 歡迎%s老師進(jìn)入學(xué)生管理系統(tǒng): 1. 添加學(xué)生 2. 查看學(xué)生 3. 修改學(xué)生信息 4. 刪除學(xué)生 5. 返回❀❀ ❀❀================================

執(zhí)行效果

Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)

到此這篇關(guān)于Python實(shí)現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)的文章就介紹到這了,更多相關(guān)Python學(xué)生管理系統(tǒng)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产午夜精品一区二区三区在线观看 | 九七午夜剧场福利写真 | 黄色片av | 成人在线视频观看 | cao在线| 北条麻妃一区二区三区在线观看 | 99久久久久久久久 | 成人网av | 亚洲国产精品第一区二区 | 一区二区三区四区在线免费观看 | 日韩欧美在线视频 | 毛片入口 | 欧美精品1区 | 97影院在线午夜 | 91麻豆精品国产91久久久更新资源速度超快 | 色黄视频在线 | 国产精品久久久久久久久久久久久久 | www..99re| 日日操日日干 | 成人在线黄色 | 国产精品一区二区久久 | 91精品入口蜜桃 | 毛片在线视频 | 国产乱码精品一区二三赶尸艳谈 | 久久久91精品国产一区二区三区 | 免费成人在线网站 | 欧美日韩久久久久 | 一区二区视频 | 一区天堂 | wwwxxx国产 | 久久一区二区三区四区 | 亚洲综合色丁香婷婷六月图片 | 亚洲精品福利视频 | 日韩三区在线 | wwwww在线观看 | 久久久免费在线观看 | 国产精品国产三级国产a | 在线国产中文字幕 | 久久久久国产一区二区三区四区 | 天天天天天操 | 久久一区二区三区四区 |