用python写一个可以录入学生信息、录入学生成绩、查询学生信息、查询学生成绩、修改学生成绩、删除学生成绩的系统并解释
时间: 2023-12-03 09:04:31 浏览: 82
好的,这是一个比较完整的学生信息管理系统,主要功能包括:
1. 录入学生信息:可以通过输入学生的基本信息,如姓名、性别、年龄、学号等,将学生信息保存到数据文件中。
2. 录入学生成绩:可以通过输入学生的学号、科目名称、成绩等信息,将学生成绩保存到数据文件中。
3. 查询学生信息:可以通过输入学生的姓名、学号等信息,从数据文件中查找并显示学生的基本信息。
4. 查询学生成绩:可以通过输入学生的学号、科目名称等信息,从数据文件中查找并显示学生成绩。
5. 修改学生成绩:可以通过输入学生的学号、科目名称等信息,修改学生成绩。
6. 删除学生成绩:可以通过输入学生的学号、科目名称等信息,从数据文件中删除学生成绩。
下面是一个示例代码:
```python
import json
# 定义一个保存学生信息和成绩的字典
students = {}
# 加载已有的数据文件
try:
with open('data.json', 'r') as f:
students = json.load(f)
except FileNotFoundError:
pass
# 定义一个函数,用于保存数据到文件中
def save_data():
with open('data.json', 'w') as f:
json.dump(students, f)
# 录入学生信息
def add_student_info():
name = input('请输入学生姓名:')
gender = input('请输入学生性别:')
age = input('请输入学生年龄:')
student_id = input('请输入学生学号:')
students[student_id] = {'name': name, 'gender': gender, 'age': age, 'scores': {}}
save_data()
print('学生信息录入成功!')
# 录入学生成绩
def add_student_score():
student_id = input('请输入学生学号:')
if student_id not in students:
print('该学生不存在!')
return
subject = input('请输入科目名称:')
score = input('请输入成绩:')
students[student_id]['scores'][subject] = score
save_data()
print('成绩录入成功!')
# 查询学生信息
def search_student_info():
keyword = input('请输入要查询的关键字:')
for student_id, student_info in students.items():
if keyword in student_id or keyword in student_info['name']:
print('学生姓名:', student_info['name'])
print('学生性别:', student_info['gender'])
print('学生年龄:', student_info['age'])
print('学生学号:', student_id)
# 查询学生成绩
def search_student_score():
student_id = input('请输入学生学号:')
if student_id not in students:
print('该学生不存在!')
return
for subject, score in students[student_id]['scores'].items():
print('科目名称:', subject)
print('成绩:', score)
# 修改学生成绩
def modify_student_score():
student_id = input('请输入学生学号:')
if student_id not in students:
print('该学生不存在!')
return
subject = input('请输入要修改的科目名称:')
if subject not in students[student_id]['scores']:
print('该科目不存在!')
return
new_score = input('请输入新的成绩:')
students[student_id]['scores'][subject] = new_score
save_data()
print('成绩修改成功!')
# 删除学生成绩
def delete_student_score():
student_id = input('请输入学生学号:')
if student_id not in students:
print('该学生不存在!')
return
subject = input('请输入要删除的科目名称:')
if subject not in students[student_id]['scores']:
print('该科目不存在!')
return
del students[student_id]['scores'][subject]
save_data()
print('成绩删除成功!')
# 主程序
while True:
print('请选择操作:')
print('1. 录入学生信息')
print('2. 录入学生成绩')
print('3. 查询学生信息')
print('4. 查询学生成绩')
print('5. 修改学生成绩')
print('6. 删除学生成绩')
print('0. 退出程序')
choice = input()
if choice == '1':
add_student_info()
elif choice == '2':
add_student_score()
elif choice == '3':
search_student_info()
elif choice == '4':
search_student_score()
elif choice == '5':
modify_student_score()
elif choice == '6':
delete_student_score()
elif choice == '0':
break
else:
print('输入错误,请重新输入!')
```
这个程序使用了一个字典来保存学生信息和成绩,通过 JSON 格式将数据保存到文件中,实现了对学生信息和成绩的录入、查询、修改和删除等功能。
阅读全文
相关推荐

















