_init_.py 主體入口
from student_manager_system1.dl_t import * # 控制登錄注冊封包
from student_manager_system1.gl_s import * # 管理學(xué)生功能封包
if __name__ == '__main__':
while True:
print("="*100)
print('學(xué)生管理系統(tǒng)-教師端'.center(100,' '))
print('\n\n1.登錄\n\n2.注冊\n\n3.退出'.center(100,' '))
print("="*100)
order = input('輸入命令:')
if order == '1':
user = input('username:')
pwd = input('password:')
if login(user,pwd):
print('login success')
while True:
print('=' * 100)
title_sys = '學(xué)生信息管理系統(tǒng)'
title_fun1 = '1 添加學(xué)生'
title_fun2 = '2 查看學(xué)生'
title_fun3 = '3 刪除學(xué)生'
title_fun4 = '4 立即退出'
print(title_sys.center(100), end='\n\n')
print(title_fun1.center(100))
print(title_fun2.center(100))
print(title_fun3.center(100))
print(title_fun4.center(100))
print('=' * 100)
order = input('請輸入您的命令:')
if order == '1':
while True:
name = input('輸入學(xué)生姓名:')
age = input('輸入學(xué)生年齡:')
sid = input('輸入學(xué)生學(xué)號:')
stu_add(name, age, sid, user)
add_ok = input('是否繼續(xù)添加y/n:')
if add_ok == 'n':
break
elif order == '2':
all_info(user)
elif order == '3':
while True:
del_name = input('請輸入學(xué)生學(xué)號:')
del_stu(del_name,user)
del_ok = input('是否繼續(xù)刪除y/n:')
if del_ok == 'n':
break
pass
else:
break
else:
continue
elif order == '2':
print('please input your infomation:')
user = input('username:')
pwd = input('password:')
while True:
p_c = input('put password again:')
if p_c != pwd:
print('input again error!')
continue
else:
break
regist(user,pwd)
continue
elif order == '3':
break
else:
continue
dl_t.py
import json
def login(user,pwd):
with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
context = json.load(teacher)
if {user:pwd} not in context:
print('please check your username or password!we will return')
return False
else:
print('login in >>>>')
return True
def regist(user,pwd):
with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
context = json.load(teacher)
context.append({user:pwd})
with open('./s_m_files/teacher.json','w',encoding='utf-8') as teacher:
json.dump(context,teacher)
print('registed success')
return True
gl_s.py
import json
def stu_add(name,age,sid,teacher):
student_info = {'name': None, 'sid': None, 'age': None}
student_info['name'] = name
student_info['age'] = age
student_info['sid'] = sid
try:
with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as add_s:
students = json.load(add_s)
except FileNotFoundError:
students = []
with open('./s_m_files/'+teacher+'.json','w', encoding='utf-8') as add_s:
students.append(student_info)
json.dump(students,add_s)
print('添加成功')
def all_info(teacher): # 所有學(xué)生信息查詢
try:
with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as a_info_s:
students = json.load(a_info_s)
if len(students) == 0:
print('沒有學(xué)生!')
else:
for x in students:
for y in x:
print(y + ':' + x[y], end=' ')
print()
except FileNotFoundError:
print('沒有學(xué)生!')
def del_stu(del_name,teacher): # 學(xué)生信息刪除
try:
with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as d_s:
students = json.load(d_s)
if len(students) == 0:
print('沒有學(xué)生!')
elif del_name not in [x['sid'] for x in students]:
print('沒有該學(xué)生')
else:
for x in students[:]:
if x['sid'] == del_name:
students.remove(x) # 獲取字典在列表中的位置,使用remove刪除
with open('./s_m_files/' + teacher + '.json', 'w', encoding='utf-8') as d_s:
json.dump(students,d_s)
print('刪除成功')
except FileNotFoundError:
print('沒有學(xué)生!')
實(shí)現(xiàn)了教師登錄,注冊功能,實(shí)現(xiàn)添加學(xué)生,刪除學(xué)生,查看所有學(xué)生功能,因?yàn)樾薷男畔⒑蛦蝹€查找的實(shí)現(xiàn)類似于添加學(xué)生和刪除學(xué)生,這就沒寫,主要思想就是登陸方用字典存儲{user : pwd},學(xué)生用字典外套列表實(shí)現(xiàn)存儲[{name : xxx , sid : xxxxx, age : xx}],同時為每個登錄名分配一個學(xué)生列表的json數(shù)據(jù)文件,當(dāng)然可以使用一個文件搞定,但是數(shù)據(jù)顯得臃腫龐大,舉一個文件的思路最外層使用列表形式[],里面以字典為元素,而這個元素里面存放3個鍵值對分別是登錄者的登錄號,密碼,和他管理的學(xué)生,其中學(xué)生項(xiàng)的值用字典外套列表表示。大概樣子是[{user:xx , pwd:xxxx , students:[{},{},{}]}],有興趣的可以一試,就是遍歷的彎彎有點(diǎn)多,需要耐心排錯。
原諒我不寫注釋,但是我基本把功能分明了,每一個函數(shù)拆開都能看,應(yīng)該比較好理解。
文件結(jié)構(gòu):

image.png
效果圖
C:\Users\FL5600\PycharmProjects\venv\Scripts\python.exe C:/Users/FL5600/PycharmProjects/student_manager_system1/__init__.py
====================================================================================================
學(xué)生管理系統(tǒng)-教師端
1.登錄
2.注冊
3.退出
====================================================================================================
輸入命令:2
please input your infomation:
username:ssss
password:12345678
put password again:1234567
input again error!
put password again:12345678
registed success
====================================================================================================
學(xué)生管理系統(tǒng)-教師端
1.登錄
2.注冊
3.退出
====================================================================================================
輸入命令:1
username:ssss
password:12345678
login in >>>>
login success
====================================================================================================
學(xué)生信息管理系統(tǒng)
1 添加學(xué)生
2 查看學(xué)生
3 刪除學(xué)生
4 立即退出
====================================================================================================
請輸入您的命令:1
輸入學(xué)生姓名:jack
輸入學(xué)生年齡:23
輸入學(xué)生學(xué)號:2018001
添加成功
是否繼續(xù)添加y/n:y
輸入學(xué)生姓名:tony
輸入學(xué)生年齡:24
輸入學(xué)生學(xué)號:2018002
添加成功
是否繼續(xù)添加y/n:n
====================================================================================================
學(xué)生信息管理系統(tǒng)
1 添加學(xué)生
2 查看學(xué)生
3 刪除學(xué)生
4 立即退出
====================================================================================================
請輸入您的命令:2
name:jack sid:2018001 age:23
name:tony sid:2018002 age:24
====================================================================================================
學(xué)生信息管理系統(tǒng)
1 添加學(xué)生
2 查看學(xué)生
3 刪除學(xué)生
4 立即退出
====================================================================================================
請輸入您的命令:3
請輸入學(xué)生學(xué)號:2018005
沒有該學(xué)生
是否繼續(xù)刪除y/n:y
請輸入學(xué)生學(xué)號:2018001
刪除成功
是否繼續(xù)刪除y/n:n
====================================================================================================
學(xué)生信息管理系統(tǒng)
1 添加學(xué)生
2 查看學(xué)生
3 刪除學(xué)生
4 立即退出
====================================================================================================
請輸入您的命令:2
name:tony sid:2018002 age:24
====================================================================================================
學(xué)生信息管理系統(tǒng)
1 添加學(xué)生
2 查看學(xué)生
3 刪除學(xué)生
4 立即退出
====================================================================================================
請輸入您的命令:4
====================================================================================================
學(xué)生管理系統(tǒng)-教師端
1.登錄
2.注冊
3.退出
====================================================================================================
輸入命令:1
username:ghjk
password:235678
please check your username or password!we will return
====================================================================================================
學(xué)生管理系統(tǒng)-教師端
1.登錄
2.注冊
3.退出
====================================================================================================
輸入命令:3
Process finished with exit code 0
更新相關(guān):
- 學(xué)號自動生成,在s_m_files中創(chuàng)建一個studnt_count.txt文件存儲學(xué)生數(shù)量,不管哪個老師添加學(xué)生都要在生成學(xué)號前先讀取studnt_count.txt中的數(shù)量,在添加學(xué)生后,數(shù)量+1再寫入,關(guān)鍵代碼如下:
file_manager.py
import json
def txtfile_read(filename):
while True:
try:
with open(filename,'r',encoding='utf-8') as txtfile:
content = txtfile.read()
print('讀取成功')
return content
break
except FileNotFoundError:
order = input('文件未找到,是否創(chuàng)建?y/n:')
if order == 'y':
f = open(filename,'w',encoding='utf-8')
f.close()
order1 = input('是否寫入內(nèi)容?y/n:')
if order1 == 'y':
new = input('輸入寫入內(nèi)容:')
with open(filename,'w',encoding='utf-8') as txtfile:
txtfile.write(new)
print('寫入成功!')
else:
filename = input('請重新輸入路徑:')
def txtfile_write(filename,title):
with open(filename,'w',encoding='utf-8') as txtfile:
txtfile.write(str(title))
print('寫入成功')
gl_s中的添加學(xué)生的方法
from student_manager_system1.file_manager import txtfile_read,txtfile_write
def stu_add(name,age,teacher):
student_info = {'name': None, 'sid': None, 'age': None}
student_info['name'] = name
student_info['age'] = age
student_count = int(txtfile_read('./s_m_files/student_count.txt')) # 讀取已經(jīng)存在的所有學(xué)生數(shù)量
student_counts = student_count + 1 # 添加一個學(xué)生就+1(因?yàn)樯蓪W(xué)號,就是刪除學(xué)生該值也不會變小)
student_info['sid'] = '2018'+str(student_counts).rjust(4,'0') # 自動生成
txtfile_write('./s_m_files/student_count.txt',student_counts) # 覆蓋寫入
try:
with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as add_s:
students = json.load(add_s)
except FileNotFoundError:
students = []
with open('./s_m_files/'+teacher+'.json','w', encoding='utf-8') as add_s:
students.append(student_info)
json.dump(students,add_s)
print('添加成功')
- 注冊名重復(fù)判斷:主要思想是
context = json.load(teacher)
{user:pwd}.keys() in [x.keys() for x in context]
判斷輸入的鍵是否存在于已經(jīng)保存各登錄名的鍵中
dl_t.py
def regist(user,pwd):
with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
context = json.load(teacher)
if {user:pwd}.keys() in [x.keys() for x in context]:
print('this username is exited!')
return False
else:
context.append({user:pwd})
with open('./s_m_files/teacher.json','w',encoding='utf-8') as teacher:
json.dump(context,teacher)
print('registed success')
return True
打包exe操作(帶清屏功能):

image.png

image.png

image.png