我正在创建一个随机测验生成器,它从.csv文件中获取其信息。这是一个较大项目的一部分,所以现在我使用一个虚拟文件,看看在哪里可以得到。我现在遇到的第一个问题是,例如,我有20个问题的CSV文件,如果我选择使用确切的问题数运行脚本,它将始终返回一个错误,指出该元素中没有足够的元素。列表(请记住,我有20个问题,我想创建一组20个问题,它可以与其他任何小于此的问题完美结合)。那是给初学者的。
第二,我不仅希望将问题随机化,而且还将答案随机化,并且我希望它返回一个标记指南,为评估者提供正确的答案,但是我真的想不出一种方法,如果我也将答案随机化。
在下面查看我的代码
import random
import csv
import os
with open('questionbank.csv') as csv_file:
readCSV = csv.reader(csv_file, delimiter=',')
number_of_lines = int(sum(1 for row in csv_file) + 1)
number_of_questions_in_file = number_of_lines / 5
print('The number of questions available in the bank is {}\n'.format(int(number_of_questions_in_file)))
l = list(range(1, number_of_lines, 5))
random.shuffle(l)
number_of_questions_requested = int(input('how many questions? '))
if number_of_questions_requested < number_of_questions_in_file:
for i in range(0, number_of_questions_requested):
question_number = int(l[i])
a1 = question_number + 1
a2 = question_number + 2
a3 = question_number + 3
correct = question_number + 4
answers = [a1, a2, a3]
# random.shuffle(answers)
print(
"Question {}: {} \n a) {} \n b) {} \n c) {} \n Correct answer is: {}\n".format(i+1, question_number,
answers[0], answers[1],
answers[2],correct))
print()
else:
print('Please choose a number below the maximum available questions in the bank. The program is terminating now')
exit()
有什么想法吗?我一直收到这些错误,我知道那一定是真的很愚蠢,但是我无法解决这个问题!