🌌 專注Golang,Python語言,云原生,人工智能領域得博主;
💜 過去經歷的意義在于引導你,而非定義你;
📢 歡迎點贊 👍 收藏 ?留言!

前言
- 學生管理系統檔案版
學生管理系統檔案版
import os
# 定義學生串列,保存所有的學生資訊
stu_list = []
def show_menu():
print('1. 添加學生')
print('2. 洗掉學生')
print('3. 修改學生資訊')
print('4. 查詢單個學生資訊')
print('5. 查詢所有的學生資訊')
print('6. 退出系統')
def insert_student():
# 1. 通過 input 函式獲取學生的資訊, 姓名, 年齡, 性別
name = input('請輸入學生名字:')
# [{}, {}, {}] 判斷的是字典中的 value 是否存在
for stu in stu_list:
if stu['name'] == name:
print('----------學生資訊存在---------')
return # 結束函式的執行
age = input('請輸入學生年齡:')
gender = input('請輸入學生性別:')
# 2. 將學生資訊轉換為字典進行保存
stu_dict = {'name': name, 'age': int(age), 'gender': gender}
# 3. 將這個學生字典添加的串列中
stu_list.append(stu_dict)
print('==============學生資訊添加成功====================')
def remove_student():
# 1. 使用 input 獲取要洗掉 /修改/查詢 的學生姓名
name = input('請輸入要操作的學生的名字:')
# 2. 判斷學生資訊是否存在
for stu in stu_list:
if stu['name'] == name:
# 3. 學生存在,對學生進行 洗掉 /修改/查詢 操作
stu_list.remove(stu)
# return
break
# 4. 學生資訊不存在,直接結束
else:
print('***********該學生資訊不存在,無法洗掉**************')
def modify_student():
# 1. 使用 input 獲取要洗掉 /修改/查詢 的學生姓名
name = input('請輸入要操作的學生的名字:')
# 2. 判斷學生資訊是否存在
for stu in stu_list:
if stu['name'] == name:
# 3. 學生存在,對學生進行 洗掉 /修改/查詢 操作
stu['age'] = int(input('請輸入新的年齡:'))
# return
break
# 4. 學生資訊不存在,直接結束
else:
print('***********該學生資訊不存在,無法修改**************')
def search_student():
# 1. 使用 input 獲取要洗掉 /修改/查詢 的學生姓名
name = input('請輸入要操作的學生的名字:')
# 2. 判斷學生資訊是否存在
for stu in stu_list:
if stu['name'] == name:
# 3. 學生存在,對學生進行 洗掉 /修改/查詢 操作
print(f'姓名:{stu["name"]}, 年齡:{stu["age"]}, 性別:{stu["gender"]}')
# return
break
# 4. 學生資訊不存在,直接結束
else:
print('***********該學生資訊不存在**************')
def show_all_info():
if len(stu_list) > 0:
for stu in stu_list:
print(f'姓名:{stu["name"]}, 年齡:{stu["age"]}, 性別:{stu["gender"]}')
# print(stu)
else:
print('目前沒有學生資訊')
def save():
# 1. 打開檔案
f = open('student.txt', 'w', encoding='utf-8')
f.write(str(stu_list))
f.close()
def load_file():
global stu_list
if os.path.exists('student.txt'):
f = open('student.txt', 'r', encoding='utf-8')
buf = f.read()
if buf:
stu_list = eval(buf)
f.close()
def main():
load_file() # 只執行一次
while True:
show_menu()
opt = input('請輸入用來選擇的操作編號:')
if opt == '1':
# print('1. 添加學生')
insert_student()
elif opt == '2':
# print('2. 洗掉學生')
remove_student()
elif opt == '3':
# print('3. 修改學生資訊')
modify_student()
elif opt == '4':
# print('4. 查詢單個學生資訊')
search_student()
elif opt == '5':
# print('5. 查詢所有的學生資訊')
show_all_info()
elif opt == '6':
print('歡迎下次使用本系統......')
save()
break
else:
print('輸入有誤,請再次輸入')
continue
input('...... 回車鍵繼續操作.......')
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/330322.html
標籤:python
