使用python的列表和字典输入3个学生的3科成绩,不用函数
时间: 2024-09-22 20:05:31 浏览: 54
你可以使用Python的基本数据结构——列表和字典来表示三名学生各科的成绩,如下所示:
```python
# 定义一个字典,键是科目,值是列表,用于存储每个学生的成绩
students_grades = {
"语文": [],
"数学": [],
"英语": []
}
# 输入三个学生的姓名和各科成绩
student1 = input("请输入第一个学生的姓名:")
for subject in students_grades.keys():
grade = float(input(f"请输入{student1}的{subject}成绩:"))
students_grades[subject].append(grade)
student2 = input("请输入第二个学生的姓名:")
for subject in students_grades.keys():
grade = float(input(f"请输入{student2}的{subject}成绩:"))
students_grades[subject].append(grade)
student3 = input("请输入第三个学生的姓名:")
for subject in students_grades.keys():
grade = float(input(f"请输入{student3}的{subject}成绩:"))
students_grades[subject].append(grade)
print(students_grades)
```
在这个例子中,我们先创建了一个空字典来存放成绩,然后通过循环获取每位学生的姓名和每门学科的成绩,并将其添加到对应的列表中。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![](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)