1.讀寫文件
import json
# 普通文本文件的讀操作
def text_read(file_name: str):
"""
普通文本文件的讀操作
:param file_name: 要讀的文件的名字
:return: 讀到的內(nèi)容
"""
try:
with open('./files/'+file_name, encoding='utf-8') as f:
return f.read()
except FileNotFoundError:
print('%s文件不存在' % (file_name))
return None
# 將文本寫入指定的文件中
def text_write(file_name: str, content: str):
"""
將文本寫入指定的文件中
:param file_name: 文件名
:param content: 要寫入到文件中的內(nèi)容
:return: 是否寫入成功
"""
with open('./files/'+file_name, 'w', encoding='utf-8') as f:
f.write(content)
return True
# 讀json文件中的內(nèi)容
def json_read(file_name: str):
"""
獲取json文件中的內(nèi)容
:param file_name: 文件名
:return: python類型的數(shù)據(jù)
"""
try:
with open('./files/'+file_name, encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
# print('%s文件不存在!' % file_name)
return None
def json_write(file_name: str, content):
"""
將內(nèi)容寫入文件中
:param file_name:
:param content:
:return: 寫入是否成功
"""
try:
with open('./files/'+file_name, 'w', encoding='utf-8') as f:
json.dump(content, f)
return True
except:
# print('寫入失敗')
return False
2.登錄主頁
import module.file_manager as file_manager
import student_manager
# 注冊
def register():
"""注冊"""
# 控制輸入賬號
while True:
# 1.輸入賬號
user_name = input('請輸入賬號(3~6位):')
# 2.檢測賬號的位數(shù)是否符合要求
if not 3 <= len(user_name) <= 6:
print('賬號有誤!請重新輸入!')
continue
else:
break
# 控制輸入密碼
while True:
# 3.在賬號輸入成功的前提下輸入密碼
pass_word = input('請輸入密碼(6~12位):')
# 4.檢測密碼是否合格
if not 6 <= len(pass_word) <= 12:
print('密碼有誤!請重新輸入!')
continue
else:
break
# 檢測賬號是否已經(jīng)注冊過!
"""
數(shù)據(jù)設(shè)計(jì):用一個(gè)字典來保存所有的賬號和密碼,賬號:密碼 -->
將保存所有的賬號和密碼對應(yīng)的字典存到一個(gè)文件中 --> 選json文件
{用戶名:密碼}
"""
# 1.獲取之前注冊過的所有的賬號信息
all_user = file_manager.json_read('all_user.json')
# 2.判斷輸入成功的賬號是否存在
if user_name in all_user:
print('注冊失?。≡撚脩裘呀?jīng)被注冊了!')
return
# 3.賬號可用,將賬號信息添加到所有的用戶中
all_user[user_name] = pass_word
# 4.更新本地文件
re = file_manager.json_write('all_user.json', all_user)
if re:
print('注冊成功!')
else:
print('注冊失敗!')
# 登錄
def login():
# 1.輸入賬號和密碼
user_name = input('請輸入賬號:')
pass_word = input('請輸入密碼:')
# 2.檢測賬號是否已經(jīng)注冊過
# 獲取系統(tǒng)所有的用戶
all_user = file_manager.json_read('all_user.json')
# 判斷賬號是否存在
if user_name not in all_user:
print('登錄失敗! 賬號不存在!')
return
# 賬號存在就先通過賬號去獲取正確的密碼
pass_word_old = all_user[user_name]
if pass_word == pass_word_old:
print('登錄成功!')
# 登錄成功后要做什么,寫在這兒....
student_manager.show_manager_page(user_name)
else:
print('登錄失??!密碼錯(cuò)誤!')
def show_main_page():
"""
顯示主頁
"""
while True:
# 顯示頁面
print(file_manager.text_read('main_page.txt'))
# 選擇
value = input('請選擇(1-3):')
# 根據(jù)不同的選擇做出不同的反應(yīng)
if value == '1':
# print('登錄功能')
login()
elif value == '2':
# print('注冊功能')
# 注冊
register()
elif value == '3':
return
else:
print('輸入有誤!')
if __name__ == '__main__':
# 1.顯示登錄注冊頁面
show_main_page()
3.學(xué)生管理系統(tǒng)
"""__author__ = 余婷"""
import module.file_manager as file_manager
# 全局變量用來保存當(dāng)前登錄成功的用戶名
current_user = None
"""
數(shù)據(jù)結(jié)構(gòu):
1.賬號和賬號管理的學(xué)生要一一對應(yīng)
aaa stu1, stu2
bbb stu3
2.一個(gè)賬號要管理多個(gè)學(xué)生
3.一個(gè)學(xué)生要存儲多個(gè)信息。每個(gè)學(xué)生存儲的信息數(shù)量一樣
方案一:整個(gè)管理系統(tǒng)所有的賬號管理的學(xué)生放在一起
{
賬號1:[{'name': name1,'age': age, 'tel':tel1, 'id':id1},...],
賬號2:[學(xué)生1, 學(xué)生2],
賬號3:[]...
...
}
將大字典寫到一個(gè)json文件
方案二:
一個(gè)賬號對應(yīng)一個(gè)json文件,每個(gè)json文件中就是一個(gè)列表
json文件名和賬號名一一對應(yīng)
aaa.json :
{
"count":0, # 當(dāng)前系統(tǒng)已經(jīng)添加的學(xué)生數(shù)目
"all_students":[{'name': name1,'age': age, 'tel':tel1, 'id':id1},...]
}
"""
# 通過全局變量保存常用的key
key_count = 'count'
key_all_students = 'all_students'
key_name = 'name'
key_age = 'age'
key_tel = 'tel'
key_id = 'id'
def get_current_userinfo():
"""獲取當(dāng)前賬號對應(yīng)的json文件中的內(nèi)容"""
content = file_manager.json_read(current_user+'.json')
if not content:
content = {}
return content
# 添加學(xué)生
def add_student():
"""添加學(xué)生"""
# 0.獲取當(dāng)前賬號的數(shù)據(jù)
user_info = get_current_userinfo()
while True:
# 1.輸入學(xué)生信息
name = input('請輸入學(xué)生的姓名:')
age = input('請輸入學(xué)生的年齡:')
tel = input('請輸入學(xué)生的電話:')
# 2.產(chǎn)生學(xué)號
number = user_info.get(key_count, 0)
number += 1
stu_id = 'stu' + str(number).rjust(3, '0')
# 3.創(chuàng)建學(xué)生
stu = {key_name: name, key_age: age, key_tel: tel, key_id: stu_id}
# 4.添加學(xué)生
all_students = user_info.get(key_all_students, [])
all_students.append(stu)
# 5.將新的數(shù)據(jù)更新到文件中
user_info[key_count] = number
user_info[key_all_students] = all_students
result = file_manager.json_write(current_user+'.json', user_info)
if result:
print('添加成功!')
else:
print('添加失?。?)
# 6.給出選擇
print('1. 繼續(xù)')
print('2. 返回')
value = input('請選擇(1-2):')
if value == '1':
continue
else:
return
def show_student_info(stu):
"""顯示一個(gè)學(xué)生的信息"""
print('學(xué)號:%s 姓名:%s 年齡:%s 電話:%s'\
% (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
def find_student_with_name(name, all_students):
"""按姓名查找學(xué)生"""
find_students = [] # 保存符合條件的所有學(xué)生
for stu in all_students:
if stu[key_name] == name:
find_students.append(stu)
return find_students
def find_student_with_id(stu_id, all_students):
"""按學(xué)號去查找學(xué)生"""
for stu in all_students:
if stu[key_id] == stu_id:
return stu
return None
def find_student():
"""查找學(xué)生"""
# 1.拿到所有學(xué)生
user_info = get_current_userinfo()
all_students = user_info.get(key_all_students, None)
if not all_students:
print('當(dāng)前賬號沒有可管理的學(xué)生!')
return
while True:
print('1.查看所有學(xué)生\n2.按姓名查找\n3.按學(xué)號查找\n4.返回')
value = input('請選擇(1-4):')
if value == '1':
for stu in all_students:
show_student_info(stu)
elif value == '2':
name = input('請輸入姓名:')
students = find_student_with_name(name, all_students)
if students:
for stu in students:
show_student_info(stu)
else:
print('沒有找到學(xué)生%s!' % name)
elif value == '3':
stu_id = input('請輸入學(xué)號:')
stu = find_student_with_id(stu_id, all_students)
if stu:
show_student_info(stu)
else:
print('沒有找到學(xué)號%s對應(yīng)的學(xué)生!' % stu_id)
else:
return
def delete_student():
"""刪除學(xué)生"""
# 1.拿到所有學(xué)生
user_info = get_current_userinfo()
all_students = user_info.get(key_all_students, [])
if not all_students:
print('當(dāng)前賬號沒有可管理的學(xué)生!')
return
while True:
# 2.給出選擇
print('1.按姓名刪除\n2.按學(xué)號刪除\n3.返回')
value = input('請選擇(1-3):')
# 3.根據(jù)不同的選擇做不同的事
if value == '1':
name = input('請輸入姓名:')
# 找到同名的所有學(xué)生
students = find_student_with_name(name, all_students)
# 判斷系統(tǒng)中是否有該學(xué)生
if not students:
print('該學(xué)生不存在!')
continue
# 如果找到了學(xué)生,先展示找到的所有學(xué)生
index = 0
for stu in students:
print(index, end=' ')
show_student_info(stu)
index += 1
print('q 返回上一層')
# 問想要具體刪除哪個(gè)學(xué)生
del_index = input('請輸入要?jiǎng)h除的學(xué)生對應(yīng)的標(biāo)號:')
if del_index == 'q':
continue
else:
# 找到要?jiǎng)h除的學(xué)生對應(yīng)的字典
del_stu = students[int(del_index)]
# 從所有的學(xué)生對應(yīng)的列表中去刪除
all_students.remove(del_stu)
# 將新的數(shù)據(jù)更新到j(luò)son文件中
result = file_manager.json_write(current_user+'.json', user_info)
if result:
print('刪除成功!')
else:
print('刪除失??!')
elif value == '2':
del_id = input('輸入要?jiǎng)h除的學(xué)號:')
# 根據(jù)學(xué)號查找學(xué)生
del_stu = find_student_with_id(del_id, all_students)
if not del_stu:
print('沒有該學(xué)生!')
continue
show_student_info(del_stu)
value = input('是否刪除(Y/N):')
if value == 'Y':
all_students.remove(del_stu)
else:
continue
# 刪除后更新數(shù)據(jù)
result = file_manager.json_write(current_user+'.json', user_info)
if result:
print('刪除成功!')
else:
print('刪除失敗!')
else:
return
# 顯示管理頁面
def show_manager_page(user_name):
"""
顯示學(xué)生管理頁面首頁
:return:
"""
# 1.保存登錄成功的用名
global current_user
current_user = user_name
# 2.顯示頁面
while True:
content = file_manager.text_read('manager_page.txt')
print(content % user_name)
# 3.做選擇
value = input('請選擇(1-5):')
if value == '5':
return
elif value == '1':
add_student()
elif value == '2':
# print('查看學(xué)生')
find_student()
elif value == '3':
print('修改學(xué)生信息')
elif value == '4':
# print('刪除學(xué)生')
delete_student()
else:
print('輸入有誤!')
4.主頁樣式
* * * * * * * * * * * * * * * * * * * * * * *
* ??歡迎來到xx學(xué)生管理系統(tǒng)?? *
* 1. 登 錄 *
* 2. 注 冊 *
* 3. 退 出 *
* * * * * * * * * * * * * * * * * * * * * * *
5.學(xué)生管理系統(tǒng)界面
* * * * * * * * * * * * * * * * * * * *
* ?歡迎%s進(jìn)入學(xué)生管理系統(tǒng) ? *
* 1.添加學(xué)生的信息 *
* 2.查找學(xué)生的信息 *
* 3.修改學(xué)生的信息 *
* 4.刪除學(xué)生的信息 *
* 5.退出學(xué)生管理系統(tǒng) *
* * * * * * * * * * * * * * * * * * * *