用PYTHON设计一个学生成绩管理系统,要求能够添加数据、查询数据、修改数据、删除数据,具有数据录入、计算、按数值排序、最高分和最低分计算等功能。信息输入和输出要简洁明了,增加必要的提示等,界面友好
时间: 2024-10-20 08:12:26 浏览: 18
为了创建一个简单的学生成绩管理系统,我们可以使用Python的内置模块如`csv`来处理数据文件,以及`argparse`库来提供命令行交互界面。以下是这个系统的基本设计:
```python
import csv
from operator import itemgetter
class StudentRecord:
def __init__(self, name, score):
self.name = name
self.score = score
def read_data(file_path):
with open(file_path, 'r', newline='') as file:
reader = csv.reader(file)
data = [StudentRecord(row[0], float(row[1])) for row in reader]
return data
def write_data(data, file_path):
with open(file_path, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Score'])
writer.writerows([(student.name, student.score) for student in data])
def add_student(data, new_student):
data.append(new_student)
def update_score(data, name, new_score):
for student in data:
if student.name == name:
student.score = new_score
break
def delete_student(data, name):
data = [student for student in data if student.name != name]
def find_scores(data, operation='min', value=None):
scores = sorted(data, key=lambda x: x.score, reverse=operation == 'max')
if value:
return [student for student in scores if student.score == value]
else:
return scores
# 主程序入口
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Student Management System")
subparsers = parser.add_subparsers(title="Commands")
# 添加学生
add_parser = subparsers.add_parser("add", help="Add a new student")
add_parser.add_argument("name", type=str, help="Student's name")
add_parser.add_argument("score", type=float, help="Student's score")
add_parser.set_defaults(func=add_student)
# 查询数据
query_parser = subparsers.add_parser("query", help="Query students by name or score")
query_parser.add_argument("option", choices=["all", "name", "score"], help="Option to search")
query_parser.add_argument("value", nargs="?", type=str, help="Value to match")
query_parser.set_defaults(func=lambda args: print(find_scores(read_data("students.csv"), args.option, args.value)))
# 修改分数
modify_parser = subparsers.add_parser("modify", help="Modify a student's score")
modify_parser.add_argument("name", type=str, help="Student's name")
modify_parser.add_argument("new_score", type=float, help="New score")
modify_parser.set_defaults(func=update_score)
# 删除学生
delete_parser = subparsers.add_parser("delete", help="Delete a student")
delete_parser.add_argument("name", type=str, help="Student's name")
delete_parser.set_defaults(func=delete_student)
args = parser.parse_args()
data = read_data("students.csv")
args.func(data, **vars(args))
# 提示用户操作结果
if 'func' in vars(args):
print(f"Operation completed. Data saved at 'students.csv'.")
```
在这个系统中,你可以通过命令行执行如下操作:
- `python manage.py add <name> <score>`: 添加新学生
- `python manage.py query all|name <value>|score <value>`: 查询所有学生、按名字或分数筛选
- `python manage.py modify <name> <new_score>`: 更新学生分数
- `python manage.py delete <name>`: 删除学生
请注意,你需要先运行一次`python manage.py add`来初始化数据文件。另外,这个例子假设所有的输入都是有效的,实际应用中需要加入错误检查和异常处理。
阅读全文