写一个python的学生信息管理系统,要完整的代码,要求能存储学号、姓名、联系方式、性别、入学年份,要求有单独的录入学生成绩的模块,要录入python,Java,网络编程课的成绩
时间: 2024-02-16 19:03:30 浏览: 46
用Python写一个学生信息管理系统
5星 · 资源好评率100%
好的,以下是一个简单的Python学生信息管理系统的完整代码,包括了学生信息的录入、查看、修改和删除功能,以及成绩录入和查看功能。代码中使用了文件存储数据,可以在程序退出后保存数据,也可以在程序启动时读取已有数据。
```python
# -*- coding:utf-8 -*-
import os
def display_menu():
"""显示菜单"""
print('===============================')
print('=========学生信息管理系统========')
print('===============================')
print('1. 录入学生信息')
print('2. 查看学生信息')
print('3. 修改学生信息')
print('4. 删除学生信息')
print('5. 录入学生成绩')
print('6. 查看学生成绩')
print('0. 退出系统')
print('===============================')
def input_student():
"""录入学生信息"""
print('请输入学生信息,按Q返回上级菜单')
students = []
while True:
name = input('请输入姓名:')
if name.upper() == 'Q':
return
if not name:
print('姓名不能为空!')
continue
std_no = input('请输入学号:')
if not std_no:
print('学号不能为空!')
continue
if len(std_no) != 8:
print('学号必须是8位数字!')
continue
if not std_no.isdigit():
print('学号必须是数字!')
continue
while True:
sex = input('请输入性别(男/女):')
if sex.upper() in ('M', '男'):
sex = '男'
break
elif sex.upper() in ('F', '女'):
sex = '女'
break
elif sex.upper() == 'Q':
return
else:
print('性别输入有误,请重新输入!')
phone = input('请输入联系方式:')
if not phone:
print('联系方式不能为空!')
continue
if len(phone) != 11:
print('联系方式必须是11位数字!')
continue
if not phone.isdigit():
print('联系方式必须是数字!')
continue
while True:
year = input('请输入入学年份(4位数字):')
if year.upper() == 'Q':
return
if not year.isdigit():
print('入学年份必须是数字!')
continue
if len(year) != 4:
print('入学年份必须是4位数字!')
continue
break
student = {
'name': name,
'std_no': std_no,
'sex': sex,
'phone': phone,
'year': year,
'courses': {
'Python': '',
'Java': '',
'网络编程': ''
}
}
students.append(student)
print('学生信息录入成功!\n')
if input('是否继续录入(Y/N):').upper() == 'N':
break
save_students(students)
def save_students(students):
"""保存学生信息"""
with open('students.txt', 'w', encoding='utf-8') as f:
for student in students:
f.write(','.join([
student['name'],
student['std_no'],
student['sex'],
student['phone'],
student['year'],
student['courses']['Python'],
student['courses']['Java'],
student['courses']['网络编程'],
]))
f.write('\n')
def load_students():
"""加载学生信息"""
if not os.path.exists('students.txt'):
return []
students = []
with open('students.txt', 'r', encoding='utf-8') as f:
for line in f:
data = line.strip().split(',')
student = {
'name': data[0],
'std_no': data[1],
'sex': data[2],
'phone': data[3],
'year': data[4],
'courses': {
'Python': data[5],
'Java': data[6],
'网络编程': data[7]
}
}
students.append(student)
return students
def show_students():
"""显示学生信息"""
students = load_students()
if not students:
print('暂无学生信息!\n')
else:
print('学生信息如下:')
print('-' * 60)
print('{:<10}{:<10}{:<10}{:<15}{:<10}{:<10}{:<10}{:<10}'.format('姓名', '学号', '性别', '联系方式', '入学年份', 'Python', 'Java', '网络编程'))
print('-' * 60)
for student in students:
print('{:<10}{:<10}{:<10}{:<15}{:<10}{:<10}{:<10}{:<10}'.format(
student['name'],
student['std_no'],
student['sex'],
student['phone'],
student['year'],
student['courses']['Python'],
student['courses']['Java'],
student['courses']['网络编程'],
))
print('-' * 60)
def find_student(std_no):
"""查找学生信息"""
students = load_students()
for student in students:
if student['std_no'] == std_no:
return student
return None
def update_student():
"""修改学生信息"""
std_no = input('请输入要修改的学生学号:')
student = find_student(std_no)
if not student:
print('未找到该学生信息!\n')
return
name = input('请输入姓名({}):'.format(student['name']))
if name:
student['name'] = name
sex = input('请输入性别({}):'.format(student['sex']))
if sex:
student['sex'] = sex
phone = input('请输入联系方式({}):'.format(student['phone']))
if phone:
student['phone'] = phone
year = input('请输入入学年份({}):'.format(student['year']))
if year:
student['year'] = year
save_students(load_students())
print('学生信息修改成功!\n')
def delete_student():
"""删除学生信息"""
std_no = input('请输入要删除的学生学号:')
student = find_student(std_no)
if not student:
print('未找到该学生信息!\n')
return
students = load_students()
students.remove(student)
save_students(students)
print('学生信息删除成功!\n')
def input_score():
"""录入学生成绩"""
print('请输入学生成绩,按Q返回上级菜单')
while True:
std_no = input('请输入学生学号:')
if std_no.upper() == 'Q':
return
student = find_student(std_no)
if not student:
print('未找到该学生信息,请重新输入!')
continue
python = input('请输入Python课程成绩:')
if python.upper() == 'Q':
return
if not python.isdigit():
print('输入成绩有误,请重新输入!')
continue
java = input('请输入Java课程成绩:')
if java.upper() == 'Q':
return
if not java.isdigit():
print('输入成绩有误,请重新输入!')
continue
network = input('请输入网络编程课程成绩:')
if network.upper() == 'Q':
return
if not network.isdigit():
print('输入成绩有误,请重新输入!')
continue
student['courses']['Python'] = python
student['courses']['Java'] = java
student['courses']['网络编程'] = network
save_students(load_students())
print('成绩录入成功!\n')
if input('是否继续录入(Y/N):').upper() == 'N':
break
def show_score():
"""显示学生成绩"""
students = load_students()
if not students:
print('暂无学生信息!\n')
else:
print('学生成绩如下:')
print('-' * 50)
print('{:<10}{:<10}{:<10}{:<10}'.format('姓名', '学号', 'Python', 'Java', '网络编程'))
print('-' * 50)
for student in students:
print('{:<10}{:<10}{:<10}{:<10}{:<10}'.format(
student['name'],
student['std_no'],
student['courses']['Python'],
student['courses']['Java'],
student['courses']['网络编程'],
))
print('-' * 50)
def main():
while True:
display_menu()
choice = input('请选择操作(0-6):')
if choice == '1':
input_student()
elif choice == '2':
show_students()
elif choice == '3':
update_student()
elif choice == '4':
delete_student()
elif choice == '5':
input_score()
elif choice == '6':
show_score()
elif choice == '0':
print('感谢使用学生信息管理系统,再见!')
break
else:
print('输入有误,请重新输入!')
if __name__ == '__main__':
main()
```
希望对你有所帮助!
阅读全文