Python实现的简易学生管理系统1.0
Python实现的简易学生管理系统 版本1.0import os
print("tyh学生管理系统,版本:",end="")
class MyClass:
def __init__(self, value):
self._value = value
@property
def value(self):
return self._value
VER = MyClass(1.0)
print(VER.value)
# 打印菜单
def print_menu():
print("=" * 30)
print("1. 添加学生")
print("2. 删除学生")
print("3. 修改学生信息")
print("4. 查询学生")
print("5. 列出所有学生信息")
print("6. 清屏(仅限双击运行时使用)")
print("0. 退出系统")
print("=" * 30)
# 添加学生
def add_student(students):
username = input("请输入用户名:")
password = input("请输入密码:")
student_id = input("请输入学号:")
uid = len(students) + 1# 确保uid唯一
for student in students:
if student["username"] == username:
print("用户名已存在,请重新输入")
return
if student["student_id"] == student_id:
print("学号已存在,请重新输入")
return
student = {"username": username, "password": password, "student_id": student_id, "uid": uid}
students.append(student)
with open("Students.txt", "a") as f:
f.write(f"{uid},{username},{password},{student_id}\n")
print("添加成功!")
# 删除学生
def delete_student(students):
student_id = input("请输入学号:")
password = input("请输入密码:")
for student in students:
if student["student_id"] == student_id:
if student["password"] == password:
students.remove(student)
with open("Students.txt", "w") as f:
for s in students:
f.write(f"{s['uid']},{s['username']},{s['password']},{s['student_id']}\n")
print("删除成功!")
return
else:
print("密码错误,请重新输入")
return
print("学号不存在,请重新输入")
# 修改学生信息
def modify_student(students):
student_id = input("请输入学号:")
for student in students:
if student["student_id"] == student_id:
if input("请输入密码:") == student["password"]:
username = input("请输入新的用户名(按回车不修改):")
if username:
for s in students:
if s["username"] == username and s["student_id"] != student_id:
print("用户名已存在,请重新输入")
return
student["username"] = username
password = input("请输入新的密码(按回车不修改):")
if password:
student["password"] = password
student_id = input("请输入新的学号(按回车不修改):")
if student_id:
for s in students:
if s["student_id"] == student_id and s["uid"] != student["uid"]:
print("学号已存在,请重新输入")
return
student["student_id"] = student_id
with open("Students.txt", "w") as f:
for s in students:
f.write(f"{s['uid']},{s['username']},{s['password']},{s['student_id']}\n")
print("修改成功!")
return
else:
print("密码错误!")
return
print("学号不存在,请重新输入")
# 查询学生
def query_student(students):
keyword = input("请输入查询关键字:")
for student in students:
if keyword == student["username"] or keyword == student["student_id"] or keyword == str(student["uid"]):
print(f"用户名:{student['username']}\t学号:{student['student_id']}\tuid:{student['uid']}")#密码:{student['password']}
print("查询完毕!")
# 列出所有学生信息
def list_students(students):
print("所有学生信息如下:")
for student in students:
print("UID: {},\t 用户名: {},\t 学号: {}".format(student["uid"], student["username"], student["student_id"], student["password"]))
# 读取文件中已有学生信息
def read_file():
try:
with open("Students.txt", "r") as f:
students = eval(f.read())
except FileNotFoundError:
save_file()
except:
students=[]
try:
with open("Students.txt", "r") as f:
st = f.read()
print(st)
with open("temp.txt","w") as f:
f.write(st)
print("程序出现错误,已备份文件,现在学生已清空,可联系作者恢复")
except:
print("程序出现严重错误,未备份文件,现在学生已清空,数据不可恢复,如有必要,联系作者")
return students
# 保存已有学生信息
def save_file():
try:
with open("Students.txt", "w") as f:
f.write(str(students))
except:
pass
return None
students = read_file()
while True:
print_menu()
temp=input("请输入命令:")
if temp=="1":
add_student(students)
elif temp=='2':
delete_student(students)
elif temp=='3':
modify_student(students)
elif temp=='4':
query_student(students)
elif temp=='5':
list_students(students)
elif temp=='6':
os.system('cls' if os.name == 'nt' else 'clear')
print("tyh学生管理系统,版本:",end="")
print(VER.value,end="")
elif temp=='0':
save_file()
break
else:
print("无效命令!")
print()
save_file() QwindF 发表于 2023-11-17 12:07
很不错,命名和分类都挺规范的
讲个笑话,我当时学的时候变量全部用拼音代替,过一天就忘记的差不多了。
...
哈哈哈好的 有没有Python不需要付费的? 有很多呀 关键词搜索 有没有学习资料 学生们的福音呐哈哈感谢分享 感谢大佬分享,最近正在学习Python语言 关键是数据库的稳定和便捷 wan456 发表于 2023-7-30 12:07
关键是数据库的稳定和便捷
Students.txt数据库保存在Students.txt 哇,Python用处好多,感谢分享 最好还是能和数据库连接