python编写教师信息管理系统
时间: 2023-07-03 11:27:17 浏览: 143
好的,教师信息管理系统可以分为以下几个模块:
1. 数据存储模块:用于存储教师信息的数据,可以选择使用文件或数据库进行存储。
2. 用户交互模块:用于与用户进行交互,包括输入输出、菜单选择等。
3. 数据处理模块:用于对输入的数据进行处理,包括添加、删除、修改、查询等操作。
下面是一个简单的Python代码实现,用于实现教师信息的添加、删除、修改、查询等操作:
```python
import sqlite3
# 数据库连接
conn = sqlite3.connect('teacher.db')
cursor = conn.cursor()
# 创建教师信息表
cursor.execute('''
CREATE TABLE IF NOT EXISTS teacher (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
gender TEXT,
subject TEXT
)
''')
# 添加教师信息
def add_teacher():
name = input('请输入教师姓名:')
age = int(input('请输入教师年龄:'))
gender = input('请输入教师性别:')
subject = input('请输入教师所教科目:')
cursor.execute('INSERT INTO teacher (name, age, gender, subject) VALUES (?, ?, ?, ?)', (name, age, gender, subject))
conn.commit()
print('添加成功!')
# 删除教师信息
def delete_teacher():
id = int(input('请输入要删除的教师ID:'))
cursor.execute('DELETE FROM teacher WHERE id = ?', (id,))
conn.commit()
print('删除成功!')
# 修改教师信息
def update_teacher():
id = int(input('请输入要修改的教师ID:'))
name = input('请输入教师姓名:')
age = int(input('请输入教师年龄:'))
gender = input('请输入教师性别:')
subject = input('请输入教师所教科目:')
cursor.execute('UPDATE teacher SET name = ?, age = ?, gender = ?, subject = ? WHERE id = ?', (name, age, gender, subject, id))
conn.commit()
print('修改成功!')
# 查询教师信息
def query_teacher():
id = int(input('请输入要查询的教师ID:'))
cursor.execute('SELECT * FROM teacher WHERE id = ?', (id,))
teacher = cursor.fetchone()
print('ID:', teacher[0])
print('姓名:', teacher[1])
print('年龄:', teacher[2])
print('性别:', teacher[3])
print('所教科目:', teacher[4])
# 菜单
while True:
print('1. 添加教师信息')
print('2. 删除教师信息')
print('3. 修改教师信息')
print('4. 查询教师信息')
print('5. 退出')
choice = int(input('请输入您的选择:'))
if choice == 1:
add_teacher()
elif choice == 2:
delete_teacher()
elif choice == 3:
update_teacher()
elif choice == 4:
query_teacher()
elif choice == 5:
break
else:
print('您的输入有误,请重新输入!')
# 关闭数据库连接
conn.close()
```
在上面的代码中,我们使用了SQLite数据库来存储教师信息,使用了Python内置的sqlite3模块来连接数据库和执行SQL语句。同时,使用了Python的基本语法和控制流程语句来实现用户交互和数据处理。你可以根据自己的需求进行修改和完善。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)