python使用字典描述学生信息,包括sno(学号),name(姓名),score(成绩),使用列表存储学生信息。在增加学生时要进行学号检查,学号不重复才能加入。根据学号查找到某位学生,对其姓名进行修改。根据学号找到某位学生,对其信息进行删除。
时间: 2024-02-13 19:01:18 浏览: 67
好的,这是一个比较常见的Python编程问题,以下是解答:
```python
students = []
def add_student(sno, name, score):
for student in students:
if student['sno'] == sno:
print('学号已存在,无法添加')
return
students.append({'sno': sno, 'name': name, 'score': score})
print('添加成功')
def find_student(sno):
for student in students:
if student['sno'] == sno:
return student
return None
def modify_name(sno, name):
student = find_student(sno)
if student:
student['name'] = name
print('修改成功')
else:
print('该学号不存在')
def delete_student(sno):
student = find_student(sno)
if student:
students.remove(student)
print('删除成功')
else:
print('该学号不存在')
```
使用方法:
1. 添加学生信息:
```python
add_student('001', '张三', 90)
```
2. 查找学生信息:
```python
student = find_student('001')
if student:
print(student)
else:
print('该学号不存在')
```
3. 修改学生姓名:
```python
modify_name('001', '李四')
```
4. 删除学生信息:
```python
delete_student('001')
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.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)