要求实现增删改查、按名字首字母排序、按成绩排序
时间: 2023-08-09 15:04:26 浏览: 50
好的,这是一个比较常见的学生信息管理系统的需求。以下是一个简单的 Python 实现,包括增删改查、按名字首字母排序、按成绩排序的功能:
```python
# 学生信息管理系统
# 支持增删改查、按名字首字母排序、按成绩排序
students = []
# 添加学生信息
def add_student():
name = input("请输入学生姓名:")
score = input("请输入学生成绩:")
student = {"name": name, "score": score}
students.append(student)
print("添加成功!")
# 删除学生信息
def del_student():
name = input("请输入要删除的学生姓名:")
for student in students:
if student["name"] == name:
students.remove(student)
print("删除成功!")
return
print("未找到该学生!")
# 修改学生信息
def update_student():
name = input("请输入要修改的学生姓名:")
for student in students:
if student["name"] == name:
score = input("请输入修改后的学生成绩:")
student["score"] = score
print("修改成功!")
return
print("未找到该学生!")
# 查询学生信息
def query_student():
name = input("请输入要查询的学生姓名:")
for student in students:
if student["name"] == name:
print("姓名:{},成绩:{}".format(student["name"], student["score"]))
return
print("未找到该学生!")
# 按名字首字母排序
def sort_by_name():
students.sort(key=lambda x: x["name"])
print("按名字首字母排序成功!")
# 按成绩排序
def sort_by_score():
students.sort(key=lambda x: x["score"])
print("按成绩排序成功!")
# 显示菜单
def show_menu():
print("=== 学生信息管理系统 ===")
print("1. 添加学生信息")
print("2. 删除学生信息")
print("3. 修改学生信息")
print("4. 查询学生信息")
print("5. 按名字首字母排序")
print("6. 按成绩排序")
print("0. 退出系统")
print("======================")
# 主函数
def main():
while True:
show_menu()
choice = input("请输入您的选择:")
if choice == "1":
add_student()
elif choice == "2":
del_student()
elif choice == "3":
update_student()
elif choice == "4":
query_student()
elif choice == "5":
sort_by_name()
elif choice == "6":
sort_by_score()
elif choice == "0":
print("感谢使用,再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == '__main__':
main()
```
你可以将这个代码保存为 `.py` 文件,然后在终端或命令提示符中运行。希望能帮到你!
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)