import os
def menuInfo():
print('==================學生管理系統====================')
print('--------------------功能菜單---------------------')
print(' 1、錄入學生資訊')
print(' 2、查詢學生資訊')
print(' 3、删除學生資訊')
print(' 4、修改學生資訊')
print(' 5、排序')
print(' 6、統計學生人數')
print(' 7、顯示所有學生資訊')
print(' 0、退出')
print('------------------------------------------------')
stu_file = 'student.txt'
def main():
while True:
menuInfo()
choice = int(input('請選擇編号:'))
if choice == 0:
answer = input('您确定要退出嗎?y/n:')
if answer == 'y' or answer == 'Y':
print('感謝您的使用!')
break # 退出系統
else:
continue
elif choice == 1:
insert()
elif choice == 2:
query()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
calcTotalNum()
elif choice == 7:
show()
def insert():
student_list = []
while True:
id = input('請輸入ID(如1001):')
if not id:
break
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
for item in student_old:
d = dict(eval(item))
if d['id'] == id:
print('該ID已存在!')
insert()
return
name = input('請輸入姓名:')
if not name:
break
try:
english = int(input('請輸入英語成績:'))
python = int(input('請輸入python成績:'))
java = int(input('請輸入Java成績:'))
except:
print('輸入無效,不是整數類型,請重新輸入')
continue
# 将錄入的資訊存到字典中
student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
# 将學生資訊添加到清單中
student_list.append(student)
answer = input('是否繼續添加?y/n:')
if answer == 'y':
continue
else:
break
# 調用save()函數
save(student_list)
print('學生資訊錄入完畢!')
def save(lst):
try:
stu_text = open(stu_file, 'a', encoding='utf-8')
except:
stu_text = open(stu_file, 'w', encoding='utf-8')
for item in lst:
stu_text.write(str(item)+'\n')
stu_text.close()
def query():
student_query = []
stu_id = ''
stu_name = ''
while True:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
answer = input('按ID查找請輸入1,按姓名查找請輸入2:')
if answer == '1':
stu_id = input('請輸入學生ID:')
elif answer == '2':
stu_name = input('請輸入學生姓名:')
else:
print('輸入不規範,請重新輸入')
query()
for item in students:
d = dict(eval(item))
if d['name'] == stu_name and stu_name:
student_query.append(d)
elif d['id'] == stu_id and stu_id:
student_query.append(d)
# 顯示查詢結果
show_student(student_query)
# 清除清單
student_query.clear()
answer = input('是否繼續查詢?y/n:')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('沒有學生資訊!!!')
return
def show_student(lst):
if len(lst) == 0:
print('未查到對應的學生資訊')
else:
# 定義顯示格式
format_style = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_style.format('ID', '姓名', 'English成績', 'Python成績', 'Java成績', '總成績'))
for item in lst:
print(format_style.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('python'),
item.get('java'),
int(item.get('english'))+int(item.get('python'))+int(item.get('java'))))
def delete():
while True:
student_id = input('請輸入需要删除的學生ID:')
if student_id:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
student_old = []
flag = False
if student_old:
with open(stu_file, 'w', encoding='utf-8') as wfile:
d = {}
for item in student_old:
d = dict(eval(item))
if d['id'] != student_id:
wfile.write(str(d)+'\n')
else:
flag = True
if flag:
print(f'id為{student_id}的學生資訊已經被删除了!')
else:
print(f'沒有找到ID為{student_id}的學生')
else:
print('沒有學生資訊')
break
show()
answer = input('是否繼續删除?y/n:')
if answer == 'y':
continue
else:
break
def modify():
while True:
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
return
student_id = input('請輸入需要修改的學生ID:')
if student_id:
if student_old:
with open(stu_file, 'w', encoding='utf-8') as wfile:
d = {}
flag = True
for item in student_old:
d = dict(eval(item))
if d['id'] == student_id:
flag = False
print('找到學生資訊,可以進行修改')
while True:
try:
englishScore = d['english']
d['english'] = input(f'原來的English成績為{englishScore},請輸入新的english成績:')
pythonScore = d['python']
d['python'] = input(f'原來的python成績為{pythonScore},請輸入新的python成績:')
javaScore = d['java']
d['java'] = input(f'原來的java成績為{javaScore},請輸入新的java成績:')
wfile.write(str(d) + '\n')
print('修改成功!')
except:
print('輸入的格式有誤,請重新輸入')
else:
break
else:
wfile.write(str(d) + '\n')
if flag:
print(f'未找到ID為{student_id}的學生資訊')
else:
print('沒有學生資訊可以被修改')
answer = input('是否繼續修改其他同學資訊?y/n:')
if answer == 'y':
continue
else:
break
def sort():
show()
stu_new = []
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
for item in students:
stu_new.append(dict(eval(item)))
else:
print('還沒有錄入學生資訊')
else:
print('還沒有錄入學生資訊')
asc_or_desc = input('請選擇(0、升序 1、降序)')
asc_or_desc_bool = False
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('輸入有誤,請重新輸入')
sort()
mode = input('請輸入排序方式(1、按照英語成績排序 2、按照python成績排序 3、按照java成績排序 0、按照總成績排序)')
if mode == '1':
stu_new.sort(key=lambda x: int(x['english']), reverse=asc_or_desc_bool)
elif mode == '2':
stu_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
elif mode == '3':
stu_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
elif mode == '0':
stu_new.sort(key=lambda x: int(x['english'])+int(x['python'])+int(x['java']), reverse=asc_or_desc_bool)
else:
print('輸入有誤,請重新輸入')
sort()
show_student(stu_new)
def calcTotalNum():
if os.path.exists(stu_file):
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
if students:
print(f'共有{len(students)}個學生')
else:
print('還沒有錄入學生資訊')
else:
print('還沒有錄入學生資訊')
def show():
if os.path.exists(stu_file):
stu_list = []
with open(stu_file, 'r', encoding='utf-8') as rfile:
students = rfile.readlines()
for item in students:
stu_list.append(dict(eval(item)))
show_student(stu_list)
else:
print('還沒有錄入學生資訊')
if __name__ == '__main__':
main()