用字典存储数据,实现一个具有基本功能的学生总成绩单,具有查询、更新、删除联系人信息功能。 具体功能要求如下: (1) 查询全部学生信息:显示所有学生的总成绩单。 (2) 插入学生成绩:可以向学生总成绩单中新建学生,若学生已经存在, 则询问是否修改学生 信息;若学生不存在,则新建学生成绩。 (3) 更新学生成绩:可以更新学生,若学生不存在,则告知。否则更新。
时间: 2024-02-03 22:14:51 浏览: 24
以下是一个简单的实现代码,使用 Python 的字典来存储学生信息:
```python
students = {}
def display_all_students():
if not students:
print("学生总成绩单为空!")
else:
print("学号\t姓名\t数学\t语文\t英语\t总分")
for student in students.values():
print("%s\t%s\t%d\t%d\t%d\t%d" % (student['id'], student['name'], student['math'], student['chinese'], student['english'], student['total']))
def add_student():
id = input("请输入学号:")
if id in students:
choice = input("该学生已存在,是否修改该学生信息?(Y/N)")
if choice == 'Y' or choice == 'y':
update_student(id)
else:
name = input("请输入姓名:")
math = int(input("请输入数学成绩:"))
chinese = int(input("请输入语文成绩:"))
english = int(input("请输入英语成绩:"))
total = math + chinese + english
students[id] = {'id': id, 'name': name, 'math': math, 'chinese': chinese, 'english': english, 'total': total}
print("添加学生信息成功!")
def update_student(id):
if id in students:
name = input("请输入姓名(回车保持不变):")
if name == '':
name = students[id]['name']
math = int(input("请输入数学成绩(回车保持不变):"))
if math == '':
math = students[id]['math']
chinese = int(input("请输入语文成绩(回车保持不变):"))
if chinese == '':
chinese = students[id]['chinese']
english = int(input("请输入英语成绩(回车保持不变):"))
if english == '':
english = students[id]['english']
total = math + chinese + english
students[id] = {'id': id, 'name': name, 'math': math, 'chinese': chinese, 'english': english, 'total': total}
print("更新学生信息成功!")
else:
print("该学生不存在!")
def delete_student(id):
if id in students:
del students[id]
print("删除学生信息成功!")
else:
print("该学生不存在!")
while True:
print("="*30)
print("学生总成绩单管理系统")
print("1. 查询全部学生信息")
print("2. 插入学生成绩")
print("3. 更新学生成绩")
print("4. 删除学生成绩")
print("0. 退出系统")
print("="*30)
choice = input("请输入你的选择:")
if choice == '1':
display_all_students()
elif choice == '2':
add_student()
elif choice == '3':
id = input("请输入要更新学生的学号:")
update_student(id)
elif choice == '4':
id = input("请输入要删除学生的学号:")
delete_student(id)
elif choice == '0':
break
else:
print("输入错误,请重新输入!")
```
在这个程序中,我们定义了四个函数来实现查询、插入、更新和删除学生信息。其中,`display_all_students` 函数用来显示所有学生的总成绩单;`add_student` 函数用来添加新的学生信息,如果学生已经存在,则询问是否修改学生信息;`update_student` 函数用来更新学生信息,如果学生不存在,则告知;`delete_student` 函数用来删除学生信息,如果学生不存在,则告知。最后,我们使用一个循环来实现程序的菜单功能,让用户可以方便地操作这个管理系统。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)