实现学生系统的增,删,查,打印所有学生功能,分别介绍使用列表存储和使用文件存储的代码
使用列表存储
import sys
students = []
def menu():
print("1. 新增学生")
print("2. 显示学生")
print("3. 查找学生")
print("4. 删除学生")
print("0. 退出程序")
choice = input("请输入您的操作>:")
return int(choice)
def insert():
print("开始新增学生!")
name = input("请输入要增加学生的姓名>:")
id = input("请输入要增加学生的学号>:")
sex = input("请输入要增加学生的性别>: 男/女")
if sex not in("男", "女"):
print("输入的性别有错!")
return
classroom = input("请输入学生所在的班级>:")
student = {
"name": name,
"id": id,
"sex": sex,
"classroom": classroom
}
global students
students.append(student)
def show():
for student in students:
print(f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
def search():
choice = int(input("请输入按姓名查找(1)/ 按学号查找(2)>: 1/2"))
if choice == 1:
name = input("请输入要查找的学生的姓名>:")
for student in students:
if student['name'] == name:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
return
print("没找到")
return
else:
id = input("请输入要查找的学生的学号>:")
for student in students:
if student['id'] == id:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
return
print("没找到")
return
def delete():
choice = int(input("请输入按姓名删除(1)/ 按学号删除(2)>: 1/2"))
if choice == 1:
name = input("请输入要删除的学生的姓名>:")
for student in students:
if student['name'] == name:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
choice2 = int(input("确认删除请按1,否则按0>:"))
if choice2 == 1:
students.remove(student)
return
print("没找到")
return
else:
id = input("请输入要删除的学生的学号>:")
for student in students:
if student['name'] == id:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
choice2 = int(input("确认删除请按1,否则按0>:"))
if choice2 == 1:
students.remove(student)
return
print("没找到")
return
def main():
print("---------------------------------")
print("-----------学生管理系统------------")
print("----student management system----")
print("---------------------------------")
while True:
choice = menu()
if choice == 1:
insert()
elif choice == 2:
show()
elif choice == 3:
search()
elif choice == 4:
delete()
elif choice == 0:
print("退出程序!")
sys.exit(0)
else:
print("输入错误,请输入0-4的数字!")
main()
使用文件存储
import os.path
import sys
students = []
def menu():
print("1. 新增学生")
print("2. 显示学生")
print("3. 查找学生")
print("4. 删除学生")
print("0. 退出程序")
choice = input("请输入您的操作>:")
return int(choice)
def save():
with open('students.txt', 'w', encoding='utf8') as f:
for student in students:
f.write(f"{student['name']}\t{student['id']}\t{student['sex']}\t{student['classroom']}\n")
print(f"存档成功! 共存储了{len(student)}条数据")
def load():
if not os.path.exists("students.txt"):
return
global students
students = []
with open("students.txt", "r", encoding="utf8") as f:
for line in f:
line = line.strip() # 去除字符串开头和末尾的空白符
tokens = line.split("\t")
if len(tokens) != 4:
print(f"第{line}行数据出现格式错误!")
continue
student = {
'id': tokens[0],
'name': tokens[1],
'sex': tokens[2],
'classroom': tokens[3]
}
students.append(student)
def insert():
print("开始新增学生!")
name = input("请输入要增加学生的姓名>:")
id = input("请输入要增加学生的学号>:")
sex = input("请输入要增加学生的性别>: 男/女")
if sex not in("男", "女"):
print("输入的性别有错!")
return
classroom = input("请输入学生所在的班级>:")
student = {
"name": name,
"id": id,
"sex": sex,
"classroom": classroom
}
global students
students.append(student)
save()
def show():
for student in students:
print(f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
def search():
choice = int(input("请输入按姓名查找(1)/ 按学号查找(2)>: 1/2"))
if choice == 1:
name = input("请输入要查找的学生的姓名>:")
for student in students:
if student['name'] == name:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
return
print("没找到")
return
else:
id = input("请输入要查找的学生的学号>:")
for student in students:
if student['id'] == id:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
return
print("没找到")
return
def delete():
choice = int(input("请输入按姓名删除(1)/ 按学号删除(2)>: 1/2"))
if choice == 1:
name = input("请输入要删除的学生的姓名>:")
for student in students:
if student['name'] == name:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
choice2 = int(input("确认删除请按1,否则按0>:"))
if choice2 == 1:
students.remove(student)
save()
return
print("没找到")
return
else:
id = input("请输入要删除的学生的学号>:")
for student in students:
if student['name'] == id:
print("找到了!")
print(
f"学生姓名:{student['name']} 学号:{student['id']} 性别:{student['sex']} 班级:{student['classroom']}")
choice2 = int(input("确认删除请按1,否则按0>:"))
if choice2 == 1:
students.remove(student)
save()
return
print("没找到")
return
def main():
load()
print("---------------------------------")
print("-----------学生管理系统------------")
print("----student management system----")
print("---------------------------------")
while True:
choice = menu()
if choice == 1:
insert()
elif choice == 2:
show()
elif choice == 3:
search()
elif choice == 4:
delete()
elif choice == 0:
print("退出程序!")
sys.exit(0)
else:
print("输入错误,请输入0-4的数字!")
main()
可以看到,这时我们的程序中就有了students.txt