吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3604|回复: 40
收起左侧

[Python 原创] Python实现的简易学生管理系统1.0

  [复制链接]
taoyuanhang 发表于 2023-7-30 10:45
Python实现的简易学生管理系统 版本1.0
[Python] 纯文本查看 复制代码
import 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()

免费评分

参与人数 5吾爱币 +10 热心值 +4 收起 理由
好先生坏印象 + 1 用心讨论,共获提升!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
ly010 + 1 谢谢@Thanks!
qiuchanghu + 1 + 1 用心讨论,共获提升!
JiaXiaoShuai + 1 + 1 热心回复!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| taoyuanhang 发表于 2023-11-18 20:10
QwindF 发表于 2023-11-17 12:07
很不错,命名和分类都挺规范的
讲个笑话,我当时学的时候变量全部用拼音代替,过一天就忘记的差不多了。
...

哈哈哈好的
红尘娇娃521 发表于 2023-7-30 11:46
追风营销 发表于 2023-7-30 11:48
AlanLee360 发表于 2023-7-30 11:49
有没有学习资料
鹿鸣 发表于 2023-7-30 11:49
学生们的福音呐哈哈感谢分享
Keran510 发表于 2023-7-30 11:56
感谢大佬分享,最近正在学习Python语言
wan456 发表于 2023-7-30 12:07
关键是数据库的稳定和便捷
id3 发表于 2023-7-30 12:17
wan456 发表于 2023-7-30 12:07
关键是数据库的稳定和便捷

Students.txt数据库保存在Students.txt
Briller 发表于 2023-7-30 12:23
哇,Python用处好多,感谢分享
hrh123 发表于 2023-7-30 12:48
最好还是能和数据库连接
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 17:58

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表