用python设计学生信息管理系统
时间: 2024-06-17 07:07:38 浏览: 22
Python可以用来设计学生信息管理系统。主要步骤如下:
1. 确定需要存储的学生信息,如姓名、学号、年龄、性别、班级等等。
2. 选择合适的数据结构存储学生信息,如列表、字典等等。
3. 设计程序界面,可以使用GUI库如Tkinter或PyQt5来实现。
4. 实现增加、删除、修改、查询等操作,需要编写相应的函数。
5. 实现数据的存储与读取,可以使用文件存储或数据库存储。
下面是一个简单的示例程序,实现了增加、删除、修改、查询等操作。它使用了字典来存储学生信息,使用文件来存储数据。
```
import os
# 定义文件名
filename = 'students.txt'
# 定义学生信息列表
students = []
# 判断文件是否存在,如果存在则读取数据
if os.path.exists(filename):
with open(filename, 'r') as f:
for line in f:
student = {}
items = line.strip().split(',')
student['name'] = items
student['id'] = items
student['age'] = items
student['gender'] = items
student['class'] = items[4]
students.append(student)
# 定义添加学生函数
def add_student():
student = {}
student['name'] = input('请输入姓名:')
student['id'] = input('请输入学号:')
student['age'] = input('请输入年龄:')
student['gender'] = input('请输入性别:')
student['class'] = input('请输入班级:')
students.append(student)
save_data()
# 定义删除学生函数
def delete_student():
id = input('请输入要删除的学生学号:')
for student in students:
if student['id'] == id:
students.remove(student)
break
save_data()
# 定义修改学生函数
def modify_student():
id = input('请输入要修改的学生学号:')
for student in students:
if student['id'] == id:
student['name'] = input('请输入新的姓名:')
student['age'] = input('请输入新的年龄:')
student['gender'] = input('请输入新的性别:')
student['class'] = input('请输入新的班级:')
break
save_data()
# 定义查询学生函数
def query_student():
id = input('请输入要查询的学生学号:')
for student in students:
if student['id'] == id:
print(f"姓名:{student['name']},年龄:{student['age']},性别:{student['gender']},班级:{student['class']}")
break
# 定义保存数据函数
def save_data():
with open(filename, 'w') as f:
for student in students:
line = ','.join([student['name'], student['id'], student['age'], student['gender'], student['class']])
f.write(line + '\n')
# 定义程序入口
def main():
while True:
print('1. 添加学生')
print('2. 删除学生')
print('3. 修改学生')
print('4. 查询学生')
print('5. 退出程序')
choice = input('请选择操作:')
if choice == '1':
add_student()
elif choice == '2':
delete_student()
elif choice == '3':
modify_student()
elif choice == '4':
query_student()
elif choice == '5':
break
if __name__ == '__main__':
main()
```
相关推荐
![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)