【发布时间】:2022-11-21 06:07:52
【问题描述】:
我有一个测试成绩的 csv 数据。当前程序能够将此数据读入二维列表,测试不合格。后来我创建了一个函数来删除行外的测试,因此只能显示学生的分数。我现在正在努力编写一个可以打印分数的函数,以便每个学生的百分比出现在单独的输出行中。
到目前为止我的代码
def getData():
with open("testscores.csv","r") as file:
lineArray = file.read().splitlines()
matrix = []
for line in lineArray:
matrix.append(line.split(","))
return matrix
def fullScores(matrix):
matrix.pop(0)
return matrix
def printscores(matrix):
for counter in matrix:
for values in counter:
print(values, end= " ")
print()
matrix = getData()
matrix = fullScores(matrix)
print()
printscores(matrix)
输出
Bob 10 9 7 8 10 9 9 9 10 8 8 10 9 9
Sue 8 8 8 9 4 8 9 7 8 3 10 10 7 9
Jan 6 6 0 5 7 9 4 7 8 5 7 1 5 9
Sam 8 8 8 7 7 7 9 9 9 9 8 9 10 8
Tom 9 9 9 9 9 9 9 9 9 10 9 9 9 9
预期产出
Bob 100% 90% 70% 80% 100% 90% 90% 90% 100% 80% 80% 100% 90% 90% Average = 89%
Sue 80% 80% 80% 90% 40% 80% 90% 70% 80% 30% 100% 100% 70% 90% Average = 77%
...
数据
Testoutof,10,11,12,11,10,11,9,10,10,11,10,12,10,9
Bob,10,9,7,8,10,9,9,9,10,8,8,10,9,9
Sue,8,8,8,9,4,8,9,7,8,3,10,10,7,9
Jan,6,6,0,5,7,9,4,7,8,5,7,1,5,9
Sam,8,8,8,7,7,7,9,9,9,9,8,9,10,8
Tom,9,9,9,9,9,9,9,9,9,10,9,9,9,9
【问题讨论】:
-
为什么只丢弃第一行?这就是告诉您应该用来计算每列百分比的数字。您似乎没有尝试计算百分比和平均值。提问前请自己尝试一下。如果您已经尝试过,您可能有比“我该怎么做?”更具体的问题,所以请问这个问题。
-
正确的。这只是为了避免向用户显示错误,但没关系,我们可以保留它。任何提示或建议如何计算百分比和平均值?谢谢。