stu={“name”:"张三","englisth":80,"python":90,"math":100} (1)请使用上述结构输入5个学生的name及3门课的成绩并存储到列表scores中。
时间: 2023-11-05 09:43:10 浏览: 194
scores = [
{"name": "张三", "english": 80, "python": 90, "math": 100},
{"name": "李四", "english": 85, "python": 95, "math": 90},
{"name": "王五", "english": 90, "python": 80, "math": 95},
{"name": "赵六", "english": 95, "python": 85, "math": 80},
{"name": "钱七", "english": 100, "python": 90, "math": 85}
]
相关问题
python 存储学生张三成绩的字典结构如下:stu={name:张三,english:80,python:90,math:100} 1)请使用上述结构输入5个学生的name及english、python和math三门课的成绩并存储到列表scores中; 2)计
1) 以下是存储5个学生成绩的代码:
```
scores = [] # 初始化一个空列表
# 循环输入5个学生的成绩
for i in range(5):
name = input("请输入第{}个学生的姓名:".format(i+1))
english = int(input("请输入{}的英语成绩:".format(name)))
python = int(input("请输入{}的Python成绩:".format(name)))
math = int(input("请输入{}的数学成绩:".format(name)))
stu = {'name': name, 'english': english, 'python': python, 'math': math} # 将学生信息存储为字典
scores.append(stu) # 将学生信息字典添加到列表中
```
2) 以下是计算每个学生的总分和平均分,并输出成绩单的代码:
```
# 计算每个学生的总分和平均分
for stu in scores:
total = stu['english'] + stu['python'] + stu['math']
avg = total / 3
stu['total'] = total # 将总分存储到字典中
stu['avg'] = avg # 将平均分存储到字典中
# 输出成绩单
print("姓名\t英语\tPython\t数学\t总分\t平均分")
for stu in scores:
print("{}\t{}\t{}\t{}\t{}\t{:.2f}".format(stu['name'], stu['english'], stu['python'], stu['math'], stu['total'], stu['avg']))
```
题目中给出了一个学生张三的成绩信息,结构如下: stu={"name":"张三","englisth":80,"python":90,"math":100} 要求使用上述结构输出5个学生的name及3门课的成绩。应该这样做: stu1={"name":"张三","englisth":80,"python":90,"math":100} stu2={"name":"李四","englisth":85,"python":95,"math":95} stu3={"name":"王五","englisth":75,"python":80,"math":90} stu4={"name":"赵六","englisth":95,"python":85,"math":80} stu5={"name":"钱七","englisth":70,"python":60,"math":75} students=[stu1,stu2,stu3,stu4,stu5] for stu in students: print(stu['name'],stu['englisth'],stu['python'],stu['math'])
好的,以下是代码实现:
```python
stu1={"name":"张三","englisth":80,"python":90,"math":100}
stu2={"name":"李四","englisth":85,"python":95,"math":95}
stu3={"name":"王五","englisth":75,"python":80,"math":90}
stu4={"name":"赵六","englisth":95,"python":85,"math":80}
stu5={"name":"钱七","englisth":70,"python":60,"math":75}
students=[stu1,stu2,stu3,stu4,stu5]
for stu in students:
print(stu['name'],stu['englisth'],stu['python'],stu['math'])
```
这段代码定义了5个学生的成绩信息,然后使用一个包含这些学生信息的列表进行遍历,输出每个学生的姓名及其3门课的成绩。
阅读全文