新生报到,需要逐个录入信息,如学生姓名、性别、专业、出生日期、家庭地址、高考成绩等。请用 实现,并实现查、删、改等功能。文本界面为: (1)新增学生信息 (2)删除学生信息 (3)导入学生信息(已保存于文件) (4)信息搜索(按姓名) (5)学生信息统计(按专业、性别或年龄) (6)按高考成绩排序 (7)学生信息保存(保存到文件) (8)退出系统
时间: 2023-11-06 18:06:58 浏览: 74
学生信息进行录入、查询、查看和修改
好的,以下是一个简单的实现,可以作为参考:
```python
import json
students = []
# 读取已保存的学生信息
def load_students():
global students
try:
with open('students.json', 'r') as f:
students = json.load(f)
except:
pass
# 保存学生信息到文件
def save_students():
with open('students.json', 'w') as f:
json.dump(students, f)
# 新增学生信息
def add_student():
name = input('请输入学生姓名:')
gender = input('请输入学生性别:')
major = input('请输入学生专业:')
birth = input('请输入学生出生日期:')
address = input('请输入学生家庭地址:')
score = input('请输入学生高考成绩:')
student = {'name': name, 'gender': gender, 'major': major, 'birth': birth, 'address': address, 'score': score}
students.append(student)
# 删除学生信息
def delete_student():
name = input('请输入要删除的学生姓名:')
for student in students:
if student['name'] == name:
students.remove(student)
print('删除成功')
return
print('未找到该学生')
# 按姓名查找学生信息
def search_student():
name = input('请输入要查找的学生姓名:')
for student in students:
if student['name'] == name:
print('姓名:', student['name'])
print('性别:', student['gender'])
print('专业:', student['major'])
print('出生日期:', student['birth'])
print('家庭地址:', student['address'])
print('高考成绩:', student['score'])
return
print('未找到该学生')
# 按专业、性别或年龄统计学生信息
def stats_student():
choice = input('请选择统计方式(1.专业 2.性别 3.年龄):')
if choice == '1':
major = input('请输入要统计的专业:')
count = 0
for student in students:
if student['major'] == major:
count += 1
print('专业为', major, '的学生人数为', count)
elif choice == '2':
gender = input('请输入要统计的性别:')
count = 0
for student in students:
if student['gender'] == gender:
count += 1
print('性别为', gender, '的学生人数为', count)
elif choice == '3':
age = int(input('请输入要统计的年龄:'))
count = 0
for student in students:
birth_year = int(student['birth'][:4])
if 2021 - birth_year == age:
count += 1
print('年龄为', age, '的学生人数为', count)
# 按高考成绩排序
def sort_student():
students.sort(key=lambda x: int(x['score']), reverse=True)
# 导入学生信息
def import_students():
filename = input('请输入要导入的文件名(格式为json):')
try:
with open(filename, 'r') as f:
data = json.load(f)
students.extend(data)
print('导入成功')
except:
print('导入失败')
# 学生信息保存
def save_student_info():
save_students()
print('保存成功')
# 主程序
if __name__ == '__main__':
load_students()
while True:
print('请选择操作:')
print('1.新增学生信息')
print('2.删除学生信息')
print('3.导入学生信息')
print('4.信息搜索(按姓名)')
print('5.学生信息统计(按专业、性别或年龄)')
print('6.按高考成绩排序')
print('7.学生信息保存')
print('8.退出系统')
choice = input('请选择:')
if choice == '1':
add_student()
elif choice == '2':
delete_student()
elif choice == '3':
import_students()
elif choice == '4':
search_student()
elif choice == '5':
stats_student()
elif choice == '6':
sort_student()
elif choice == '7':
save_student_info()
elif choice == '8':
break
```
阅读全文