登錄注冊
import file_funcs
import stu_sys
# 登錄注冊界面
def list_login():
print('''
+++++++++++++++++++++++++++++++++++++++++++++++++
+ welcome to student manage system +
+ 1.register +
+ +
+ 2.login +
+ +
+ 0.exit +
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
cmd = input('>>>>>>>')
if '0'<=cmd<='2':
return cmd
else:
print('輸入內(nèi)存錯誤绳姨!')
return False
def login():
while True:
result = list_login()
if not result:
continue
if result == '1':
user_name = input('請輸入用戶名:')
password = input('請輸入密碼:')
users = file_funcs.get_infos('users')
for index in range(len(users)):
if user_name == users[index]['name']:
print('該用戶名已經(jīng)存在!')
break
else:
new_user = {'name':user_name, 'password':password, 'database':user_name+'_database'}
file_funcs.add_info('users', new_user)
print('注冊成功!')
if result == '2':
user_name = input('請輸入用戶名:')
password = input('請輸入密碼:')
users = file_funcs.get_infos('users')
for index in range(len(users)):
if user_name == users[index]['name']:
if password == users[index]['password']:
print('登錄成功!')
stu_sys.main(user_name+'_database')
else:
print('密碼錯誤!')
break
else:
print('用戶名不存在!')
if result == '0':
return
if __name__ == '__main__':
login()
文件讀寫:
import json
# 獲取信息
def get_infos(file_name):
try:
with open('./files/'+file_name+'.json', 'r', encoding='utf-8') as f:
all_stu_infos = json.load(f)
except:
all_stu_infos = []
return all_stu_infos
# 添加信息
def add_info(file_name,stu_info):
# 獲取之前的學(xué)生信息
all_stu_infos = get_infos(file_name)
with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f:
# 將新的學(xué)生信息添加到所有學(xué)生信息的最后
all_stu_infos.append(stu_info)
# 保存所有信息
json.dump(all_stu_infos, f)
# 返回保存成功的信息
return True
# 重新寫入
def rewrite(file_name,stu_info):
with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f:
# 保存所有信息
json.dump(stu_info, f)
# 返回保存成功的信息
return True
if __name__ == '__main__':
pass
學(xué)生系統(tǒng)
import file_funcs
def list_main():
print('''
+++++++++++++++++++++++++++++++++++++++++++++++++
+ welcome to student manage system +
+ 1.add student information +
+ 2.View personal information by id +
+ 3.del personal information by id +
+ 4.view all information +
+ 5.view personal average score by id +
+ 0.exit +
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
user_cmd = input('>>>>>')
return user_cmd
# 判斷用戶輸入
def user_input(user_cmd):
if user_cmd == '1':
stu_info = stu_find()
if not isinstance(stu_info, str):
print('該學(xué)生信息已存在!')
return
name, stu_id, english, pe, art, math, age = stu_info_input(stu_info)
stu_create(name, stu_id, english, pe, art, math, age)
# 判斷是否繼續(xù)或者退出
flag = '1'
redo(flag)
elif user_cmd == '2':
stu_info = stu_find()
if isinstance(stu_info, str):
print('學(xué)生信息不存在舷丹!')
else:
# 打印學(xué)生信息
print('姓名:%s\t學(xué)號:%s\t英語:%s\t體育:%s\t美術(shù):%s\t數(shù)學(xué):%s\t年齡:%s\t' % (stu_info['name'],
stu_info['stu_id'],stu_info['score']['English'],stu_info['score']['PE'],
stu_info['score']['art'],stu_info['score']['math'],stu_info['age']))
# 判斷是否繼續(xù)或者退出
flag = '2'
redo(flag)
elif user_cmd == '3':
stu_info = stu_find()
if isinstance(stu_info, str):
print('學(xué)生信息不存在!')
else:
stu_del(stu_info)
# 判斷是否繼續(xù)或者退出
flag = '3'
redo(flag)
elif user_cmd == '4':
stu_find_all()
# 判斷是否繼續(xù)或者退出
flag = '4'
redo(flag)
elif user_cmd == '5':
stu_info = stu_find()
if isinstance(stu_info, str):
print('學(xué)生信息不存在蜓肆!')
else:
score_ave(stu_info)
# 判斷是否繼續(xù)或者退出
flag = '5'
redo(flag)
elif user_cmd == '0':
exit(0)
else:
print('輸入內(nèi)容有誤颜凯!')
# 查找學(xué)生
def stu_find():
stu_infos = file_funcs.get_infos(file_name)
stu_info = input('請輸入學(xué)生的學(xué)號:')
for index_info in range(len(stu_infos)):
# 學(xué)生信息存在
if stu_info == stu_infos[index_info]['stu_id']:
return stu_infos[index_info]
# 學(xué)生信息不存在
else:
return stu_info
# 查找全部
def stu_find_all():
stu_infos = file_funcs.get_infos(file_name)
for index_info in range(len(stu_infos)):
print('姓名:%s\t學(xué)號:%s\t英語:%s\t體育:%s\t美術(shù):%s\t數(shù)學(xué):%s\t年齡:%s\t' % (stu_infos[index_info]['name'],
stu_infos[index_info]['stu_id'],
stu_infos[index_info]['score']['English'],
stu_infos[index_info]['score']['PE'],
stu_infos[index_info]['score']['art'],
stu_infos[index_info]['score']['math'],
stu_infos[index_info]['age']))
# 用戶輸入學(xué)生信息
def stu_info_input(stu_info):
stu_id = stu_info
name = input('請輸入學(xué)生的姓名:')
english = input('請輸入音樂成績:')
pe = input('請輸入體育成績:')
art = input('請輸入美術(shù)成績:')
math = input('請輸入數(shù)學(xué)成績:')
age = input('請輸入學(xué)生的年齡:')
return name, stu_id, english, pe, art, math, age
# 添加學(xué)生信息
def stu_create(name, stu_id, english, pe, art, math, age):
# stu_infos.append({'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math},
# 'age':age})
file_funcs.add_info(file_name, {'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math},
'age':age})
# 刪除學(xué)生信息
def stu_del(stu_info):
stu_infos = file_funcs.get_infos(file_name)
index_stu = stu_infos.index(stu_info)
del stu_infos[index_stu]
file_funcs.rewrite(file_name, stu_infos)
print('刪除信息成功!')
# 平均成績
def score_ave(stu_info):
score_average = (int(stu_info['score']['English']) + int(stu_info['score']['PE'])
+ int(stu_info['score']['art']) + int(stu_info['score']['math'])) / 4
print('姓名:%s\t學(xué)號:%s\t平均成績是:%d' % (stu_info['name'],
stu_info['stu_id'], score_average))
#
def redo(flag):
if flag =='1':
print('''
1.繼續(xù)添加
2.返回上一層
''')
user_cmd = input('>>>>>')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('輸入有誤!')
elif flag == '2':
print('''
1.繼續(xù)查看
2.返回上一層
''')
user_cmd = input('>>>>>')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('輸入有誤!')
elif flag == '3':
print('''
1.繼續(xù)刪除
2.返回上一層
''')
user_cmd = input('>>>>>')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('輸入有誤!')
elif flag == '4':
print('''
1.繼續(xù)查看
2.返回上一層
''')
user_cmd = input('>>>>>')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('輸入有誤!')
elif flag == '5':
print('''
1.繼續(xù)查詢
2.返回上一層
''')
user_cmd = input('>>>>>')
if user_cmd == '1':
user_input(flag)
elif user_cmd == '2':
return
else:
print('輸入有誤!')
# 主程序
def main(user_name):
global file_name
file_name = user_name
global stu_infos
stu_infos = file_funcs.get_infos(user_name)
while True:
# 打印列表內(nèi)容仗扬,等待用戶輸入
user_input(list_main())
# 開始程序
if __name__ == '__main__':
main('joe_database')