【发布时间】:2020-03-31 11:20:54
【问题描述】:
我的目标: 有n个学生的记录。每个记录包含学生的姓名和他们在数学、物理、化学方面的百分比。标记可以是浮动值。您需要将记录保存在字典数据类型中。学生的名字是关键。标记将值存储在列表中。用户输入学生的姓名。输出该学生获得的平均百分比分数。 这是我的代码:
n = int(input("Enter total number of students:"))
student_marks = {}
for x in range(n):
name, *line = input().split()
scores = list(map(float, line))
student_marks[name] = scores
student_name = input("Enter name of the student whose average you want:")
print("{0:.2f}".format(round(sum(student_marks[student_name]) / len(student_marks[student_name]), 2)))
我实际上得到了这个输出:
Enter total number of students:3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Enter name of the student whose average you want:Malika
56.00
但我希望我的示例输入采用包含标记作为列表的 dict 形式,即,
Sample input: {'Krishna':[67,68,69],
'Arjun':[70,98,63],
'Malika':[52,56,60]}
如何修改我的代码以提供这样的输入?不使用导入 asl? 请帮忙。提前致谢。
【问题讨论】:
-
我不明白这个问题的目的!您可能没有注意到,但您已经将输入存储到名为
student_marks的字典数据类型中。 -
你想输入多行字典文字,还是只输入单行文字?
-
单行文字@MisterMiyagi
-
@ShreyaReddy 那么您能否提供正确的示例输入?当前示例输入是一个多行 dict 文字。
-
请澄清问题到底是什么。见How to Ask、help center。
标签: python