python课程设计报告及代码(python课程设计报告)

引言

Python大数据人工智能、机器学习等领域中都有着广泛的应用,同时基于Python编程的开源工具库也数不胜数。因此,学习Python已经成为现代软件工程师的必修课程之一。本报告主要展示我在课程设计中使用了Python语言开发的项目。

项目概述

本项目是一个基于Python语言的学生成绩管理系统,包括以下模块:学生信息管理、成绩管理、班级排名等。系统可以通过命令行的方式进行交互,支持记录学生基本信息、成绩查询、添加成绩、修改成绩、删除成绩、班级总分及排名等功能。

在本项目中,我使用了Python语言的基础语法及常用库,如:json、os、sys等。数据存储采用json文件的形式进行存储,避免使用关系型数据库的复杂操作。结合Python语言的易学易用优点,并运用自学能力,较快地完成了课程设计的任务。

代码实现

本项目的主要代码如下:

python课程设计报告及代码(python课程设计报告)

```
import os
import sys
import json
# 加载json文件
def load_json_file(file_path):
if not os.path.exists(file_path):
return None
with open(file_path, "r", encoding="utf-8") as f:
return json.load(f)

# 写入json文件
def write_json_file(file_path, data):
with open(file_path, "w", encoding="utf-8") as f:
json.dump(data, f)
# 学生信息管理
class Student(object):
def __init__(self):
self.file_path = "student.json"
self.student_data = load_json_file(self.file_path)

def get_student_data(self):
return self.student_data

def add_student(self, student_id, student_name, class_name):
student = {"id": student_id, "name": student_name, "class": class_name}
if not self.student_data:
self.student_data = [student]
else:
for item in self.student_data:
if item.get("id") == student_id:
return "Record already exists."
self.student_data.append(student)
write_json_file(self.file_path, self.student_data)
return "Operation success."
# 成绩管理
class Record(object):
def __init__(self):
self.file_path = "record.json"
self.record_data = load_json_file(self.file_path)

def get_record_data(self):
return self.record_data

def add_record(self, student_id, score):
if not self.record_data:
self.record_data = [{"student_id": student_id, "score": score}]
else:
for item in self.record_data:
if item.get("student_id") == student_id:
return "Record already exists."
self.record_data.append({"student_id": student_id, "score": score})
write_json_file(self.file_path, self.record_data)
return "Operation success."
def get_class_total(self):
class_total = {}
for item in self.record_data:
student_id = item.get("student_id")
for student in self.student_data:
if student.get("id") == student_id:
class_name = student.get("class")
if class_total.get(class_name):
class_total[class_name] = class_total[class_name] + item.get("score")
else:
class_total[class_name] = item.get("score")
return class_total
# 班级排名
def rank_class(class_data):
sorted_class_data = sorted(class_data.items(), key=lambda x: -x[1])
print("Ranking:")
for i, item in enumerate(sorted_class_data):
print("{rank}:{class_name}:{total_score}".format(rank=i + 1, class_name=item[0], total_score=item[1]))

if __name__ == "__main__":
student = Student()
record = Record()
student_data = student.get_student_data()
record_data = record.get_record_data()

print("===================================")
print("Welcome to the student score system")
print("===================================")

while True:
print("(1) Add student: add a student")
print("(2) Add score: add a score for a student")
print("(3) Get score: get a score of a student")
print("(4) Delete score: delete a score of a student")
print("(5) Class total: show class total score")
print("(6) Class rank: show class rank")
print("(0) Exit: exit the system")
operation = input("Please select operation[0-6]:")
if operation == "0":
print("Thank you for using this system!")
sys.exit()
elif operation == "1":
student_id = input("Please input student_id:")
student_name = input("Please input student_name:")
class_name = input("Please input class_name:")
result = student.add_student(student_id, student_name, class_name)
print(result)
elif operation == "2":
student_id = input("Please input student_id:")
score = int(input("Please input score:"))
result = record.add_record(student_id, score)
print(result)
elif operation == "3":
student_id = input("Please input student_id:")
if not student_data:
print("No student.")
continue
for student in student_data:
if student.get("id") == student_id:
student_name = student.get("name")
for item in record_data:
if item.get("student_id") == student_id:
print("{student_name}:{score}".format(student_name=student_name, score=item.get("score")))
elif operation == "4":
student_id = input("Please input student_id:")
if not record_data:
print("No record.")
continue
for index, item in enumerate(record_data):
if item.get("student_id") == student_id:
record_data.pop(index)
write_json_file("record.json", record_data)
print("Delete success.")
break
elif operation == "5":
class_total = record.get_class_total()
for class_name, total_score in class_total.items():
print("{class_name}:{total_score}".format(class_name=class_name, total_score=total_score))
elif operation == "6":
class_total = record.get_class_total()
rank_class(class_total)
else:
print("Wrong operation, please input again.")
```

以上Python代码实现了学生成绩管理系统的基本功能,运用了基础语法和读写文件、处理字典等常用库的操作。通过命令行进行交互,用户可以完成添加学生信息、添加成绩、查询成绩、删除成绩、班级总分及排名等操作。

总结

Python作为一种高级语言,其在软件领域的应用越来越广泛。在本项目的开发中,我提高了自己对Python语言的理解和综合应用能力。在实现过程中,我发现Python的易学易用的语言特性能够有效地提高我的开发效率,让我更加便捷地开发项目。此外,我也体会到了自学、自查资料的重要性,并从中锤炼了自己的自学能力。

本文来自投稿,不代表亲测学习网立场,如若转载,请注明出处:https://www.qince.net/pythonyy45.html

郑重声明:

本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。 若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。

我们不承担任何技术及版权问题,且不对任何资源负法律责任。

如遇到资源无法下载,请点击这里失效报错。失效报错提交后记得查看你的留言信息,24小时之内反馈信息。

如有侵犯您的版权,请给我们私信,我们会尽快处理,并诚恳的向你道歉!

(0)
上一篇 2023年4月18日 下午5:29
下一篇 2023年4月18日 下午5:29

猜你喜欢