1.从键盘上输入,添加4个学生的信息(姓名、学号,性别、分数),并打印 2.在每个学生的信息里添加身高信息(’height’:185) 3.将第二个同学的分数修改为90分 4.删除第三个同学的信息
时间: 2024-03-25 07:40:39 浏览: 18
好的,您可以使用如下的 Python 代码实现:
```python
# 添加学生信息
students = []
for i in range(4):
name = input("请输入学生姓名:")
id = input("请输入学生学号:")
sex = input("请输入学生性别:")
score = input("请输入学生分数:")
student = {"姓名": name, "学号": id, "性别": sex, "分数": score}
students.append(student)
# 添加身高信息
for student in students:
student["身高"] = 185
# 修改第二个同学的分数为90分
students[1]["分数"] = 90
# 删除第三个同学的信息
del students[2]
# 打印学生信息
for student in students:
print(student)
```
在上述代码中,我们首先使用 for 循环和 input 函数从键盘上输入了4个学生的信息,并将它们存储在一个列表 students 中。然后使用 for 循环和字典的键值对来添加每个学生的身高信息。接着使用索引操作符 [] 修改了第二个同学的分数为90分。最后使用 del 关键字删除了第三个同学的信息。最后再使用 for 循环打印了每个学生的信息。
相关问题
1. 从键盘上输入,添加4个学生的信息(姓名、学号,性别、分数),并打印 2. 在每个学生的信息里添加身高信息(’height’:185) 3. 将第二个同学的分数修改为90分 4. 删除第三个同学的信息
好的,我来逐个回答您的问题。
1. 从键盘上输入,添加4个学生的信息(姓名、学号,性别、分数),并打印,以下是Python的代码实现:
```python
# 从键盘上输入4个学生的信息,并打印
student1 = {'name': input("请输入第1个学生的姓名:"), 'id': input("请输入第1个学生的学号:"), 'sex': input("请输入第1个学生的性别:"), 'score': input("请输入第1个学生的分数:")}
student2 = {'name': input("请输入第2个学生的姓名:"), 'id': input("请输入第2个学生的学号:"), 'sex': input("请输入第2个学生的性别:"), 'score': input("请输入第2个学生的分数:")}
student3 = {'name': input("请输入第3个学生的姓名:"), 'id': input("请输入第3个学生的学号:"), 'sex': input("请输入第3个学生的性别:"), 'score': input("请输入第3个学生的分数:")}
student4 = {'name': input("请输入第4个学生的姓名:"), 'id': input("请输入第4个学生的学号:"), 'sex': input("请输入第4个学生的性别:"), 'score': input("请输入第4个学生的分数:")}
print("4个学生的信息:")
print(student1)
print(student2)
print(student3)
print(student4)
```
2. 在每个学生的信息里添加身高信息(’height’:185),以下是Python的代码实现:
```python
# 在每个学生的信息里添加身高信息(’height’:185)
student1['height'] = 185
student2['height'] = 185
student3['height'] = 185
student4['height'] = 185
print("添加身高信息后的4个学生的信息:")
print(student1)
print(student2)
print(student3)
print(student4)
```
3. 将第二个同学的分数修改为90分,以下是Python的代码实现:
```python
# 将第二个同学的分数修改为90分
student2['score'] = 90
print("修改分数后的4个学生的信息:")
print(student1)
print(student2)
print(student3)
print(student4)
```
4. 删除第三个同学的信息,以下是Python的代码实现:
```python
# 删除第三个同学的信息
del student3
print("删除第三个同学的信息后的学生的信息:")
print(student1)
print(student2)
print(student4)
```
希望这些代码能帮到您!
python1. 从键盘上输入,添加4个学生的信息(姓名、学号,性别、分数),并打印 2. 在每个学生的信息里添加身高信息(’height’:185) 3. 将第二个同学的分数修改为90分 4. 删除第三个同学的信息
以下是实现你的要求的 Python 代码:
```
# 从键盘输入四个学生的信息
student1 = {'name': input('请输入第1个学生的姓名:'), 'id': input('请输入第1个学生的学号:'), 'gender': input('请输入第1个学生的性别:'), 'score': float(input('请输入第1个学生的分数:'))}
student2 = {'name': input('请输入第2个学生的姓名:'), 'id': input('请输入第2个学生的学号:'), 'gender': input('请输入第2个学生的性别:'), 'score': float(input('请输入第2个学生的分数:'))}
student3 = {'name': input('请输入第3个学生的姓名:'), 'id': input('请输入第3个学生的学号:'), 'gender': input('请输入第3个学生的性别:'), 'score': float(input('请输入第3个学生的分数:'))}
student4 = {'name': input('请输入第4个学生的姓名:'), 'id': input('请输入第4个学生的学号:'), 'gender': input('请输入第4个学生的性别:'), 'score': float(input('请输入第4个学生的分数:'))}
# 打印学生信息
print("学生1信息:", student1)
print("学生2信息:", student2)
print("学生3信息:", student3)
print("学生4信息:", student4)
# 在每个学生信息中添加身高信息
student1['height'] = 185
student2['height'] = 185
student3['height'] = 185
student4['height'] = 185
# 将第二个同学的分数修改为90分
student2['score'] = 90
# 删除第三个同学的信息
del student3
# 打印修改后的学生信息
print("修改后的学生1信息:", student1)
print("修改后的学生2信息:", student2)
print("删除后的学生3信息:", student3) # 这里会报错,因为已经删除了该学生的信息
print("修改后的学生4信息:", student4)
```
注意:在删除第三个同学的信息时,由于 Python 是面向对象的语言,所以我们可以使用 `del` 关键字来删除对象。但是需要注意,一旦删除后,该对象就不存在了,再次使用该对象会导致错误。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)