Compare commits

...

No commits in common. 'main' and 'master' have entirely different histories.
main ... master

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.11 (PyCharmMiscProject)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11 (PyCharmMiscProject)" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/PythonProject1.iml" filepath="$PROJECT_DIR$/.idea/PythonProject1.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -1 +0,0 @@
# student

@ -0,0 +1,92 @@
time import date
from typing import List, Optional
from dal.StudentDAL import StudentDAL
from student.Student import Student
class StudentBLL:
def __init__(self, dal: StudentDAL):
self.dal = dal
def add_student(self, student: Student):
if not student.is_valid:
errors = ", ".join(student.get_errors())
raise ValueError(f"学生数据校验失败: {errors}")
if not self.dal.add_student(student):
raise ValueError("添加失败,身份证号或学号可能已存在")
def delete_student(self, stu_id: str):
if not self.dal.delete_student(stu_id):
raise ValueError(f"找不到学号为 {stu_id} 的学生")
def update_student(self, stu_id: str, student: Student):
if not student.is_valid:
errors = ", ".join(student.get_errors())
raise ValueError(f"学生数据校验失败: {errors}")
if not self.dal.update_student(stu_id, student):
raise ValueError(f"找不到学号为 {stu_id} 的学生")
def get_student_by_id_card(self, id_card: str) -> Optional[Student]:
return self.dal.get_by_id_card(id_card)
def get_student_by_stu_id(self, stu_id: str) -> Optional[Student]:
return self.dal.get_by_stu_id(stu_id)
def get_all_students(self) -> List[Student]:
return self.dal.get_all()
def search_students(self, search_type: str, keyword: str) -> List[Student]:
search_type = search_type.lower()
if search_type == 'name':
return self.dal.search_by_name(keyword)
elif search_type == 'class':
return self.dal.search_by_class(keyword)
elif search_type == 'major':
return self.dal.search_by_major(keyword)
else:
return []
def get_student_count(self) -> int:
return self.dal.get_student_count()
def get_major_counts(self) -> dict:
return self.dal.get_major_counts()
def calculate_avg_height(self) -> float:
students = self.dal.get_all()
heights = [s.height for s in students if s.height is not None]
if not heights:
return 0.0
return sum(heights) / len(heights)
def calculate_avg_weight(self) -> float:
students = self.dal.get_all()
weights = [s.weight for s in students if s.weight is not None]
if not weights:
return 0.0
return sum(weights) / len(weights)
def get_students_by_age(self, min_age: int, max_age: int) -> List[Student]:
today = date.today()
students = self.dal.get_all()
result = []
for student in students:
if student.age is None:
continue
if min_age <= student.age <= max_age:
result.append(student)
return result
def export_data(self, file_path: str) -> bool:
return self.dal.export_data(file_path)
def import_data(self, file_path: str) -> bool:
return self.dal.import_data(file_path)
def clear_all(self) -> bool:
return self.dal.clear_all()

@ -0,0 +1,157 @@
import os
import json
from datetime import date
from typing import List, Optional
from student.Student import Student
class StudentDAL:
def __init__(self, file_path: str):
self.file_path = file_path
self._ensure_file_exists()
def _ensure_file_exists(self):
"""确保文件存在"""
if not os.path.exists(self.file_path):
os.makedirs(os.path.dirname(self.file_path), exist_ok=True)
with open(self.file_path, 'w', encoding='utf-8') as f:
json.dump([], f)
def _load_data(self) -> List[dict]:
"""加载数据"""
try:
with open(self.file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return []
def _save_data(self, data: List[dict]):
"""保存数据"""
with open(self.file_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
def add_student(self, student: Student) -> bool:
data = self._load_data()
# 检查身份证号是否唯一
if any(s['id_card'] == student.id_card for s in data):
return False
# 检查学号是否唯一
if any(s['stu_id'] == student.stu_id for s in data):
return False
data.append(student.to_dict())
self._save_data(data)
return True
def get_by_id_card(self, id_card: str) -> Optional[Student]:
data = self._load_data()
for student_dict in data:
if student_dict['id_card'] == id_card:
return Student.from_dict(student_dict)
return None
def get_by_stu_id(self, stu_id: str) -> Optional[Student]:
data = self._load_data()
for student_dict in data:
if student_dict['stu_id'] == stu_id:
return Student.from_dict(student_dict)
return None
def get_all(self) -> List[Student]:
return [Student.from_dict(d) for d in self._load_data()]
def update_student(self, stu_id: str, student: Student) -> bool:
data = self._load_data()
updated = False
for i, student_dict in enumerate(data):
if student_dict['stu_id'] == stu_id:
# 保留原始身份证号
new_data = student.to_dict()
new_data['id_card'] = student_dict['id_card']
data[i] = new_data
updated = True
break
if updated:
self._save_data(data)
return updated
def delete_student(self, stu_id: str) -> bool:
data = self._load_data()
original_count = len(data)
data = [s for s in data if s['stu_id'] != stu_id]
if len(data) < original_count:
self._save_data(data)
return True
return False
def search_by_name(self, name: str) -> List[Student]:
data = self._load_data()
name_lower = name.lower()
return [
Student.from_dict(d)
for d in data
if name_lower in d['name'].lower()
]
def search_by_class(self, class_name: str) -> List[Student]:
data = self._load_data()
class_lower = class_name.lower()
return [
Student.from_dict(d)
for d in data
if d.get('class_name') and class_lower in d['class_name'].lower()
]
def search_by_major(self, major: str) -> List[Student]:
data = self._load_data()
major_lower = major.lower()
return [
Student.from_dict(d)
for d in data
if d.get('major') and major_lower in d['major'].lower()
]
def get_student_count(self) -> int:
return len(self._load_data())
def get_major_counts(self) -> dict:
data = self._load_data()
counts = {}
for student_dict in data:
major = student_dict.get('major', '未指定')
counts[major] = counts.get(major, 0) + 1
return counts
def clear_all(self) -> bool:
self._save_data([])
return True
def export_data(self, file_path: str) -> bool:
data = self._load_data()
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=4)
return True
except Exception:
return False
def import_data(self, file_path: str) -> bool:
try:
with open(file_path, 'r', encoding='utf-8') as f:
new_data = json.load(f)
current_data = self._load_data()
current_ids = {s['id_card'] for s in current_data}
# 只导入不重复的学生
to_import = [s for s in new_data if s['id_card'] not in current_ids]
self._save_data(current_data + to_import)
return True
except Exception:
return False

@ -0,0 +1,22 @@
from dal.StudentDAL import StudentDAL
from bll.StudentBLL import StudentBLL
from tui.StudentTUI import StudentTUI
def main():
# 初始化数据访问层
dal = StudentDAL("data/students.json")
# 初始化业务逻辑层
bll = StudentBLL(dal)
# 初始化用户界面
tui = StudentTUI(bll)
# 启动系统
tui.run()
if __name__ == "__main__":
main()

@ -0,0 +1,123 @@
import csv
import json
import os
import sqlite3
from abc import ABC, abstractmethod
from datetime import date
from typing import List, Optional, Union
# 学生类定义
class Student:
def __init__(self,
name: str,
id_card: str,
stu_id: str,
gender: Optional[bool] = None,
height: Optional[int] = None,
weight: Optional[float] = None,
enrollment_date: Optional[Union[date, str]] = None,
class_name: Optional[str] = None,
major: str = None):
self.name = name
self.id_card = id_card
self.stu_id = stu_id
self.gender = gender
self.height = height
self.weight = weight
if isinstance(enrollment_date, str):
self.enrollment_date = date.fromisoformat(enrollment_date)
else:
self.enrollment_date = enrollment_date
self.class_name = class_name
self.major = major
self.age, self.birthday = self._calculate_age_and_birthday()
self._validation_errors = []
self._validate_all()
def _calculate_age_and_birthday(self):
birth_year = int(self.id_card[6:10])
birth_month = int(self.id_card[10:12])
birth_day = int(self.id_card[12:14])
birthday = date(birth_year, birth_month, birth_day)
today = date.today()
age = today.year - birth_year
if (today.month, today.day) < (birth_month, birth_day):
age -= 1
return age, birthday
def _validate_all(self):
self._validate_id_card()
self._validate_stu_id()
self._validate_name()
self._validate_enrollment_date()
self._validate_height()
self._validate_weight()
def _validate_id_card(self):
if len(self.id_card) != 18:
self._validation_errors.append("身份证号必须为18位")
# 简单的校验位验证,可根据国家标准完善
try:
int(self.id_card[:17])
except ValueError:
self._validation_errors.append("身份证号前17位必须为数字")
def _validate_stu_id(self):
if not self.stu_id:
self._validation_errors.append("学号不能为空")
def _validate_name(self):
if not (2 <= len(self.name) <= 20):
self._validation_errors.append("姓名长度需在2-20个字符之间")
if any(char.isdigit() or not char.isalpha() for char in self.name):
self._validation_errors.append("姓名不能包含数字和特殊符号")
def _validate_enrollment_date(self):
if self.enrollment_date and self.enrollment_date < self.birthday:
self._validation_errors.append("入学日期不能早于出生日期")
def _validate_height(self):
if self.height is not None and not (50 <= self.height <= 250):
self._validation_errors.append("身高需在50-250cm之间")
def _validate_weight(self):
if self.weight is not None and not (5 <= self.weight <= 300):
self._validation_errors.append("体重需在5-300kg之间")
@property
def is_valid(self):
return len(self._validation_errors) == 0
def get_errors(self):
return self._validation_errors.copy()
def to_dict(self):
return {
'name': self.name,
'id_card': self.id_card,
'stu_id': self.stu_id,
'gender': self.gender,
'height': self.height,
'weight': self.weight,
'enrollment_date': self.enrollment_date.isoformat() if self.enrollment_date else None,
'class_name': self.class_name,
'major': self.major,
'age': self.age,
'birthday': self.birthday.isoformat()
}
@classmethod
def from_dict(cls, data):
return cls(
name=data['name'],
id_card=data['id_card'],
stu_id=data['stu_id'],
gender=data['gender'],
height=data['height'],
weight=data['weight'],
enrollment_date=data['enrollment_date'],
class_name=data['class_name'],
major=data['major']
)

@ -0,0 +1,292 @@
from datetime import date
from bll.StudentBLL import StudentBLL
from dal.StudentDAL import StudentDAL
from student.Student import Student
class StudentTUI:
def __init__(self, bll: StudentBLL):
self.bll = bll
def display_menu(self):
print("\n===== 学生信息管理系统 =====")
print("1. 添加学生")
print("2. 删除学生")
print("3. 更新学生")
print("4. 查询学生")
print("5. 统计分析")
print("6. 导入导出")
print("7. 清空数据")
print("0. 退出系统")
print("==========================")
def run(self):
while True:
self.display_menu()
choice = input("请选择操作: ").strip()
if choice == "0":
print("再见!")
break
elif choice == "1":
self.add_student()
elif choice == "2":
self.delete_student()
elif choice == "3":
self.update_student()
elif choice == "4":
self.query_students()
elif choice == "5":
self.show_stats()
elif choice == "6":
self.import_export()
elif choice == "7":
self.clear_data()
else:
print("无效选择,请重试")
def add_student(self):
print("\n--- 添加学生 ---")
name = input("姓名: ").strip()
id_card = input("身份证号: ").strip()
stu_id = input("学号: ").strip()
gender = self.input_gender()
height = self.input_int("身高(cm): ")
weight = self.input_float("体重(kg): ")
enrollment_date = self.input_date("入学日期(YYYY-MM-DD): ")
class_name = input("班级: ").strip()
major = input("专业: ").strip()
try:
student = Student(
name=name,
id_card=id_card,
stu_id=stu_id,
gender=gender,
height=height,
weight=weight,
enrollment_date=enrollment_date,
class_name=class_name,
major=major
)
self.bll.add_student(student)
print("添加成功!")
except ValueError as e:
print(f"添加失败: {e}")
def input_gender(self):
"""输入性别"""
while True:
gender = input("性别(1.男 2.女 3.跳过): ").strip()
if gender == '1':
return True
elif gender == '2':
return False
elif gender == '3':
return None
else:
print("无效选择,请重新输入")
def input_int(self, prompt: str):
"""输入整数"""
while True:
value = input(prompt).strip()
try:
return int(value) if value else None
except ValueError:
print("请输入有效的整数")
def input_float(self, prompt: str):
"""输入浮点数"""
while True:
value = input(prompt).strip()
try:
return float(value) if value else None
except ValueError:
print("请输入有效的数字")
def input_date(self, prompt: str):
"""输入日期"""
while True:
try:
date_str = input(prompt).strip()
if not date_str:
return None
return date.fromisoformat(date_str)
except ValueError:
print("日期格式应为YYYY-MM-DD")
def delete_student(self):
print("\n--- 删除学生 ---")
stu_id = input("请输入学号: ").strip()
try:
self.bll.delete_student(stu_id)
print("删除成功!")
except ValueError as e:
print(f"删除失败: {e}")
def update_student(self):
print("\n--- 更新学生 ---")
stu_id = input("请输入学号: ").strip()
student = self.bll.get_student_by_stu_id(stu_id)
if not student:
print("学生不存在")
return
print(f"当前信息: {student}")
name = input(f"新姓名({student.name}): ").strip() or student.name
id_card = input(f"新身份证号({student.id_card}): ").strip() or student.id_card
new_stu_id = input(f"新学号({student.stu_id}): ").strip() or student.stu_id
gender = self.input_gender_update(student.gender)
height = self.input_int(f"新身高({student.height}): ") or student.height
weight = self.input_float(f"新体重({student.weight}): ") or student.weight
enrollment_date = self.input_date_update(student.enrollment_date)
class_name = input(f"新班级({student.class_name}): ").strip() or student.class_name
major = input(f"新专业({student.major}): ").strip() or student.major
try:
updated_student = Student(
name=name,
id_card=id_card,
stu_id=new_stu_id,
gender=gender,
height=height,
weight=weight,
enrollment_date=enrollment_date,
class_name=class_name,
major=major
)
# 如果学号改变了,需要先删除旧记录
if new_stu_id != stu_id:
self.bll.delete_student(stu_id)
self.bll.add_student(updated_student)
print("更新成功!")
except ValueError as e:
print(f"更新失败: {e}")
def input_gender_update(self, current_gender):
current = "" if current_gender is True else "" if current_gender is False else "未指定"
print(f"当前性别: {current}")
return self.input_gender()
def input_date_update(self, current_date):
"""输入日期(更新时)"""
if current_date:
print(f"当前日期: {current_date.isoformat()}")
return self.input_date("新的日期(YYYY-MM-DD): ") or current_date
def query_students(self):
print("\n--- 查询学生 ---")
print("1. 按身份证号查询")
print("2. 按学号查询")
print("3. 按姓名查询")
print("4. 按班级查询")
print("5. 按专业查询")
print("6. 所有学生")
choice = input("请选择查询方式: ").strip()
students = []
if choice == "1":
id_card = input("请输入身份证号: ").strip()
student = self.bll.get_student_by_id_card(id_card)
if student:
students = [student]
elif choice == "2":
stu_id = input("请输入学号: ").strip()
student = self.bll.get_student_by_stu_id(stu_id)
if student:
students = [student]
elif choice == "3":
name = input("请输入姓名: ").strip()
students = self.bll.search_students('name', name)
elif choice == "4":
class_name = input("请输入班级: ").strip()
students = self.bll.search_students('class', class_name)
elif choice == "5":
major = input("请输入专业: ").strip()
students = self.bll.search_students('major', major)
elif choice == "6":
students = self.bll.get_all_students()
else:
print("无效选择")
return
self.print_students(students)
def print_students(self, students):
if not students:
print("没有找到学生")
return
print("\n身份证号\t学号\t姓名\t性别\t身高\t体重\t班级\t专业\t年龄")
print("=" * 100)
for s in students:
gender = "" if s.gender is True else "" if s.gender is False else "未知"
print(f"{s.id_card}\t{s.stu_id}\t{s.name}\t{gender}\t"
f"{s.height or '--'}\t{s.weight or '--'}\t"
f"{s.class_name or '--'}\t{s.major or '--'}\t"
f"{s.age or '--'}")
print(f"共找到 {len(students)} 名学生")
def show_stats(self):
print("\n--- 统计分析 ---")
print("1. 学生总数")
print("2. 专业分布")
print("3. 平均身高")
print("4. 平均体重")
print("5. 按年龄范围查询")
choice = input("请选择统计项目: ").strip()
if choice == "1":
count = self.bll.get_student_count()
print(f"学生总数: {count}")
elif choice == "2":
major_counts = self.bll.get_major_counts()
print("\n专业分布:")
for major, count in major_counts.items():
print(f" {major}: {count}")
elif choice == "3":
avg_height = self.bll.calculate_avg_height()
print(f"平均身高: {avg_height:.1f} cm")
elif choice == "4":
avg_weight = self.bll.calculate_avg_weight()
print(f"平均体重: {avg_weight:.1f} kg")
elif choice == "5":
min_age = int(input("最小年龄: "))
max_age = int(input("最大年龄: "))
students = self.bll.get_students_by_age(min_age, max_age)
self.print_students(students)
else:
print("无效选择")
def import_export(self):
print("\n--- 导入导出 ---")
print("1. 导出数据")
print("2. 导入数据")
choice = input("请选择操作: ").strip()
if choice == "1":
file_path = input("导出文件路径: ").strip()
if self.bll.export_data(file_path):
print("导出成功!")
else:
print("导出失败")
elif choice == "2":
file_path = input("导入文件路径: ").strip()
if self.bll.import_data(file_path):
print("导入成功!")
else:
print("导入失败")
else:
print("无效选择")
def clear_data(self):
confirm = input("确定要清空所有数据吗?(y/n): ").strip().lower()
if confirm == 'y':
if self.bll.clear_all():
print("数据已清空")
else:
print("清空失败")

Binary file not shown.

@ -1,280 +0,0 @@
import os.path
filename = 'students.txt'
def main():
while True:
menu()
choice = int(input('请选择功能'))
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('您确定退出系统吗y/n')
if answer == 'y' or answer == 'Y':
print("欢迎您的使用!!")
break
else:
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
input()
def menu():
print('==================学生信息管理系统==============')
print('===============功能菜单==================')
print('\t\t\t1. 录入学生信息')
print('\t\t\t2. 查找学生信息')
print('\t\t\t3. 删除学生信息')
print('\t\t\t4. 修改学生信息')
print('\t\t\t5. 对学生成绩进行排序')
print('\t\t\t6. 统计学生总人数')
print('\t\t\t7. 显示所有学生信息')
print('\t\t\t0. 退出程序')
print('======================================')
def insert():
student_list = []
while True:
id = input("请输入ID")
if not id:
break
name = input("请输入姓名:")
if not name:
break
try:
english = int(input("请输入英语成绩:"))
python = int(input("请输入python成绩"))
java = int(input("请输入Java成绩"))
except:
print("输入无效,请重新输入")
# 将录入的学生信息保存在字典中
student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
# 将学生信息添加到列表中
student_list.append(student)
answer = input("是否继续添加y/n?")
if answer == 'y':
continue
else:
break
# 调用save函数保存信息
save(student_list)
print("学生信息保存成功")
def save(lst):
try:
stu_txt = open(filename, 'a', encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in lst:
stu_txt.write(str(item) + '\n')
stu_txt.close()
def search():
student_query = []
while True:
id = ''
name = ''
if os.path.exists(filename):
mode = input("按ID查找请按1按姓名查找请按2")
if mode == '1':
id = input("请输入学生ID")
elif mode == '2':
name = input("请输入学生新明:")
else:
print("输入有误,请重新输入")
search()
with open(filename, 'r', encoding='utf-8') as file:
student = file.readlines()
for item in student:
d = dict(eval(item))
if id != '':
if d['id'] == id:
student_query.append(d)
elif name != '':
if d['name'] == name:
student_query.append(d)
show_query(student_query)
# 清空列表
student_query.clear()
answer = input("是否继续查询y/n")
if answer == 'y':
continue
else:
break
else:
print("无学生信息")
return
def show_query(lst):
if len(lst) == 0:
print("无相关信息")
return
# 定义标题显示格式
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID', '姓名', '英语成绩', 'python成绩', 'Java成绩', '总成绩'))
# 定义内容显示格式
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('english'),
item.get('english'),
item.get('java'),
int( item.get('english')) + int( item.get('english')) + int( item.get('java'))
))
def delete():
while True:
student_id = input("请输入删除学生的ID")
if student_id != '':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as file:
student_old = file.readlines()
else:
student_old = []
flag = False
if student_old: # 空列表等于 False非空列表等于True
with open(filename,'w',encoding='utf-8') as wfile:
# 只用于写入。如果该文件已存在则打开文件,并从开头开始编辑,即原有内容会被删除
d = {}
for item in student_old:
d = dict(eval(item)) # 将字符串转换为字典
if d['id'] != student_id:
wfile.write(str(d)+'\n')
else:
flag = True
if flag:
print(f'id为{student_id}的学生信息已被删除')
else:
print(f'没有找到学生ID为{student_id}的学生')
else:
print("无学生信息")
break
show()
answer = input("是否继续删除y/n")
if answer == 'y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
student_old = file.readlines()
else:
return
student_id = input("请输入修改学生的ID")
with open(filename, 'w', encoding='utf-8') as wfile:
for item in student_old:
d = {}
d = dict(eval(item))
if d['id'] == student_id:
print("已经找到学生信息,请修改相关信息")
while True:
try:
d['name'] = input("请输入姓名")
d['english'] = input("请输入英语成绩")
d['python'] = input("请输入python成绩")
d['java'] = input("请输入Java成绩")
except:
print("输入有误,请重新输入")
else:
break
wfile.write(str(d) + '\n')
print("修改成功")
else:
wfile.write(str(d)+'\n')
answer = input("是否继续修改其他学生信息y/n")
if answer == 'y':
modify()
else:
return
def sort():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding= 'utf-8') as file:
students = file.readlines()
students_new = []
for item in students:
d = dict(eval(item))
students_new.append(d)
else:
return
# 排序方式
asc_or_desc = input("请选择升序 0 还是降序 1")
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print("输入有误,请重新输入")
sort()
mode = input("请选择排序方式(1.英语 2.python 3.Java 4.总成绩)")
if mode == '1':
students_new.sort(key=lambda x : int(x['english']), reverse=asc_or_desc_bool)
# x 代表列表students_new中的项
elif mode == '2':
students_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
elif mode == '3':
students_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
elif mode == '4':
students_new.sort(key=lambda x: int(x['english']) + int(x['python']) + int(x['java']), reverse=asc_or_desc_bool)
else:
print("输入有误,请重新输入")
show_query(students_new)
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
students = file.readlines()
if students:
print(f"总共有{len(students)}名学生")
else:
print("无学生信息")
else:
print("无学生信息")
def show():
student_query = []
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
student = file.readlines()
for item in student:
d = dict(eval(item))
student_query.append(d)
show_query(student_query)
# 清空列表
student_query.clear()
else:
print("无学生信息")
return
if __name__ == '__main__':
main()
Loading…
Cancel
Save