我在学校做一个“侧边项目”,测试用户,然后将他们的结果保存到报告(csv文件)中。到目前为止,我已经做了一个报告,我正在做测试的客户(Fergus)可以搜索一个特定的用户,并在他们所做的测试中看到他们的进展,这很好,但我正在努力与第二个报告。你知道吗
对于第二份报告,弗格斯必须输入某一科目(数学、英语、科学等)的主题和难度等级(容易、难等),并应写出一份报告,说明这次测验的平均成绩。我试图通过把所有的个人分数相加,然后根据人们参加某个测验的次数来计算平均分,但在阅读文件时似乎有一个问题,因为它只是把所有的结果都读为零。也就是说,当总分要除以测验的次数时,会出现一个错误,说我试图除以零。你知道吗
def quizReport():
overall=0
amount=0
subjectInput=input("\nPlease enter the subject name of the quiz(zes) you wish to view: ")
difficultyInput=input("Please enter the difficulty level of the quiz(zes) you wish to view: ")
with open ('reportForFergusTwo.csv') as quizzReport:
quizReportReader=csv.reader(quizzReport)
for column in quizReportReader:
if subjectInput == column[0]:
if difficultyInput == column[1]:
if ('0') == column[2]:
amount=amount+1
if ('1') == column[2]:
overall=overall+1
amount=amount+1
if ('2') == column[2]:
overall=overall+2
amount=amount+1
if ('3') == column[2]:
overall=overall+3
amount=amount+1
if ('4') == column[2]:
overall=overall+4
amount=amount+1
if ('5') == column[2]:
overall=overall+5
amount=amount+1
if ('6') == column[2]:
overall=overall+6
amount=amount+1
if ('7') == column[2]:
overall=overall+7
amount=amount+1
if ('8') == column[2]:
overall=overall+8
amount=amount+1
if ('9') == column[2]:
overall=overall+9
amount=amount+1
if ('10') == column[2]:
overall=overall+10
amount=amount+1
total=overall/amount
因为找不到相应的条目,所以被0除,因此
amount
永远不会递增,所以应该处理这种情况。 而且,你的代码可以简化很多,它是非常重复的。你知道吗我无法测试,因为您没有提供csv文件的示例,但请告诉我您是否满意:
编辑:如果找不到预期的数据,则可能是您没有正确读取csv。在循环中用
print
显示line
,以确保它是您所期望的。你知道吗一个可能的原因可能是分隔符:csv有时用逗号分隔,有时用分号分隔。这是
csv.reader
的可选参数。你知道吗编辑:在我的代码中添加
strip()
,以防csv中逗号后面有空格。你知道吗相关问题 更多 >
编程相关推荐