構思
學生管理系統(tǒng) 應該包含老師注冊登錄 管理學生信息(增刪改查)還有數(shù)據(jù)持久化
因為數(shù)據(jù)存入JSON文件 增刪改查都需要讀取和修改文件
所以需要一個讀寫文件的方法文件 file_manager
密碼加密可以用到哈希模塊文件 tools
老師和學生的類文件 model
程序入口(主頁)文件index
核心增刪改查文件 student_manager
file_manager.py
"""
Project: ClassStudent
Creator: 貓貓
Create time: 2021-02-25 20:23
IDE: PyCharm
Introduction:https://blog.csdn.net/Cantevenl/article/details/115439530
"""
base_dir = 'files/' # 定義一個變量 文件路徑
# 讀文件的函數(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 json
json.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: ClassStudent
Creator: 貓貓
Create time: 2021-02-25 20:24
IDE: PyCharm
Introduction:https://blog.csdn.net/Cantevenl/article/details/115439530
"""
# 加密密碼
import hashlib
def encrypt_password(passwd, x='zhumaogouniu'):
h = hashlib.sha256()
h.update(passwd.encode('utf8'))
h.update(x.encode('utf8'))
return h.hexdigest()
model.py
"""
Project: ClassStudent
Creator: 貓貓
Create time: 2021-02-25 20:24
IDE: PyCharm
Introduction:https://blog.csdn.net/Cantevenl/article/details/115439530
"""
# 創(chuàng)建一個老師對象
class Teacher(object):
def __init__(self, name, password):
import tools
self.name = name
self.password = tools.encrypt_password(password)
class Student(object):
def __init__(self, s_id, name, age, gender, tel):
self.s_id = s_id
self.name = name
self.age = age
self.gender = gender
self.tel = tel
index.py
"""
Project: ClassStudent
Creator: 貓貓
Create time: 2021-02-25 20:25
IDE: PyCharm
Introduction:https://blog.csdn.net/Cantevenl/article/details/115439530
"""
import sys
import file_manager # 導入自己寫的讀取文件模塊
import model
import student_manager
def register():
# 讀取文件,查看文件里是否有數(shù)據(jù)。如果文件不存在,默認是一個字典
data = file_manager.read_json('data.json', {})
while True:
teacher_name = input('請輸入賬號(3~6位):')
if not 2 = len(teacher_name) = 6:
print('賬號不符合要求,請重新輸入!')
else:
break
if teacher_name in data:
print('注冊失敗!該賬號已經(jīng)被注冊!')
return
while True:
password = input('請輸入密碼(6~12位):')
if not 6 = len(password) = 12:
print('密碼不符合要求,請重新輸入!')
else:
break
# 用戶名密碼都已經(jīng)輸入正確以后 將用戶名和密碼以key:value形式儲存
# 可以創(chuàng)建一個teacher對象
t = model.Teacher(teacher_name, password)
data[t.name] = t.password
file_manager.write_json('data.json', data)
def login():
# 讀取文件,查看文件里是否有數(shù)據(jù)。如果文件不存在,默認是一個字典
data = file_manager.read_json('data.json', {})
teacher_name = input('請輸入老師賬號:')
if teacher_name not in data:
print('登錄失敗!該賬號沒有注冊!')
return
password = input('請輸入密碼:')
import tools
if data[teacher_name] == tools.encrypt_password(password):
student_manager.name = teacher_name
print('登錄成功')
student_manager.show_manager()
else:
print('密碼錯誤,登陸失敗!')
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) # 退出整個程序
sys.exit(0) # 退出程序
else:
print('輸入有誤!')
if __name__ == '__main__':
start()
student_manager.py
"""
Project: ClassStudent
Creator: 貓貓
Create time: 2021-02-25 20:26
IDE: PyCharm
Introduction:https://blog.csdn.net/Cantevenl/article/details/115439530
"""
import file_manager
import model
name = ''
# 添加
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('請輸入學生姓名:')
s_age = input('請輸入年齡:')
s_gender = input('請輸入性別:')
s_tel = input('請輸入電話號碼:')
num += 1
# 字符串的zfill方法,在字符串的前面補0
s_id = 'student_id_' + str(num).zfill(3)
# 創(chuàng)建一個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寫進去
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.查看所有學生\n2.根據(jù)姓名查找\n3.根據(jù)學號查找\n其他:返回\n請選擇:')
students = file_manager.read_json(name + '.json', {})
all_students = students.get('all_student', []) # get拿列表里的數(shù)據(jù),拿不到默認是一個空列表[]
if not all_students:
print('該老師沒有學生,請?zhí)砑訉W生')
return
if choice == '1': # 查看所有學生
pass
elif choice == '2': # 根據(jù)姓名查看
s_name = input('請輸入學生的姓名:')
all_students = filter(lambda s: s['name'] == s_name, all_students) # students就只保留了指定名字的學生
elif choice == '3': # 學號查找
s_id = input('請輸入學生的學號:')
all_students = filter(lambda s: s['s_id'] == s_id, all_students)
else:
return
if not students:
print('未找到學生')
return
for student in all_students:
print('學號:{s_id},姓名:{name},性別:{gender},年齡{age},電話號碼{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('該老師沒有學生,請?zhí)砑訉W生')
return
modify = input('1.根據(jù)姓名修改\n2.根據(jù)學號修改\n其他:返回\n請選擇:')
if modify == '1':
value = input('請輸入要修改的學生名字:')
key = 'name'
elif modify == '2':
value = input('請輸入要修改的學生學號:')
key = 's_id'
else:
return
students = list(filter(lambda s: s[key] == value, all_students))
if not all_students:
print('沒有找到對應的學生')
return
for i, student in enumerate(students):
print('{xiabiao} 學號:{s_id},姓名:{name},性別:{gender},年齡{age},電話號碼{tel}'.format(xiabiao=i, **student))
n = int(input('請輸入需要修改的學生的標號(0~{}),q-->返回:'.format(len(students) - 1)))
if not 0 = n = len(students):
print('輸入的內容錯誤')
return
all_students.remove(students[n])
students[n]['s_id'] = new_input((students[n]['s_id']), "請輸入修改后的學生學號[回車則不修改]:")
students[n]['name'] = new_input((students[n]['name']), "請輸入修改后的學生姓名[回車則不修改]:")
students[n]['gender'] = new_input((students[n]['gender']), "請輸入修改后的學生性別[回車則不修改]:")
students[n]['age'] = new_input((students[n]['age']), "請輸入修改后的學生年齡[回車則不修改]:")
students[n]['tel'] = new_input((students[n]['tel']), "請輸入修改后的學生電話號碼[回車則不修改]:")
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('該老師沒有學生,請?zhí)砑訉W生')
return
num = input('1.按照姓名刪除\n2.按照學號刪除\n其他:返回\n請選擇:')
if num == '1':
key = 'name'
value = input('請輸入要刪除的學生名字:')
elif num == '2':
key = 's_id'
value = input('請輸入要刪除的學生學號:')
else:
return
students = list(filter(lambda s: s.get(key, '') == value, all_students))
if not students:
print('沒有找到對應的學生')
return
for i, student in enumerate(students):
print('{xiabiao} 學號:{s_id},姓名:{name},性別:{gender},年齡{age},電話號碼{tel}'.format(xiabiao=i, **student))
n = input('請輸入需要刪除的學生的標號(0~{}),q-->返回:'.format(len(students) - 1))
if not n.isdigit() or not 0 = int(n) = len(students):
print('輸入的內容錯誤')
return
# 將學生從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':
break
else:
print('輸入錯誤!')
# 修改時用的input方法
def new_input(old, new):
input_str = input(new)
if len(input_str) > 0:
return input_str
else:
return old
美化文本
通過自己寫的讀取文件方法可以直接讀取文本(類似界面)
welcome.txt
================================
** 歡迎來到學生管理系統(tǒng)**
1. 登 錄
2. 注 冊
3. 退 出
** **
================================
students_page.txt
================================
❀❀ 歡迎%s老師進入學生管理系統(tǒng):
1. 添加學生
2. 查看學生
3. 修改學生信息
4. 刪除學生
5. 返回
❀❀ ❀❀
================================
執(zhí)行效果
到此這篇關于Python實現(xiàn)學生管理系統(tǒng)的代碼(JSON模塊)的文章就介紹到這了,更多相關Python學生管理系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 教你用python實現(xiàn)一個無界面的小型圖書管理系統(tǒng)
- python實現(xiàn)學生管理系統(tǒng)源碼
- 基于python實現(xiàn)銀行管理系統(tǒng)
- 基于python實現(xiàn)圖書管理系統(tǒng)
- python實現(xiàn)簡易名片管理系統(tǒng)
- python實現(xiàn)學生通訊錄管理系統(tǒng)
- 使用python實現(xiàn)學生信息管理系統(tǒng)
- 利用Python實現(xiàn)學生信息管理系統(tǒng)的完整實例
- 教你用Python實現(xiàn)簡易版學生信息管理系統(tǒng)(含源碼)