我试着把它清理一下,并重命名变量,这样会更容易理解。我还随机分配了每个学生的答案
import random
userInput = int(input("Enter a DOB:"))
questionRatio = [10,4,3,2,1]
questionsPerCategory = [int((r/20)*userInput) for r in questionRatio]
numberStudents = 10
# initialize the solutions for the questions randomly
solutions = [random.randint(1,4) for i in range(userInput)]
# initialize the answers of the students randomly
# (if the same answers for each student are needed as before change to):
# studentAnswers = [[random.randint(1,4) for i in range(userInput)] * numberStundents]
studentAnswers = [[random.randint(1,4) for i in range(userInput)] for j in range(numberStudents)]
category1 = {}
category2 = {}
category3 = {}
category4 = {}
category5 = {}
#Save correct, incorrect noOfIncorrect, totalMark per student
correctMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
noOfIncorrectForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
incorrectMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
totalMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
# we iterate over the solutions, with the indices of the solutions
for index, solution in enumerate(solutions):
# we check every studentAnswer for their answer at the current index, and compare them to the solution for current index
for studentNumber, studentAnswer in enumerate(studentAnswers):
# Print for Debug purposes
#print("Checking Student", studentNumber+1, "Index", index, studentAnswer[index], solutions[index])
if studentAnswer[index] == solution:
# For correct answers increase the correctMarks for this student by one
correctMarksForStudent[studentNumber+1] += 1
else:
# For incorrectAnswers increase the number of incorrect answers for this student by one
noOfIncorrectForStudent[studentNumber+1] += 1
# Then add to the already incorrectMarks 0.5 times the no of incorrect answers (this gets big fast)
incorrectMarksForStudent[studentNumber+1] += 0.5 * noOfIncorrectForStudent[studentNumber+1]
# update the total mark of the student by setting it to correctMark - incorrectMark
totalMarksForStudent[studentNumber+1] = correctMarksForStudent[studentNumber+1] - incorrectMarksForStudent[studentNumber+1]
# print for Debug purposes
#print("Total For Student", studentNumber+1, totalMarksForStudent[studentNumber+1])
# updating the correct Category at the student index with the new totalMarks of the student
if index < questionsPerCategory[0]:
category1[studentNumber+1] = totalMarksForStudent[studentNumber+1]
elif index < questionsPerCategory[0] + questionsPerCategory[1]:
category2[studentNumber+1] = totalMarksForStudent[studentNumber+1]
elif index < questionsPerCategory[0] + questionsPerCategory[1] + questionsPerCategory[2]:
category3[studentNumber+1] = totalMarksForStudent[studentNumber+1]
elif index < questionsPerCategory[0] + questionsPerCategory[1] + questionsPerCategory[2] + questionsPerCategory[3]:
category4[studentNumber+1] = totalMarksForStudent[studentNumber+1]
else:
category5[studentNumber+1] = totalMarksForStudent[studentNumber+1]
# printing the solutions and the student answers takes a lot of time and terminal space
#print("Solutions",solutions)
#print("StudentAnswers",studentAnswers)
print("CorrectMark",correctMarksForStudent)
print("IncorrectMarks",incorrectMarksForStudent)
print("StudentMarks",totalMarksForStudent)
print("Category1",category1)
print("Category2",category2)
print("Category3",category3)
print("Category4",category4)
print("Category5",category5)
但这仍然需要很长时间(对我来说大约需要 2 分钟),而且我不确定最终结果应该是什么样子,这就是我能从中得到的。但也许这会帮助你或其他人找出更好的方法。我不确定哪个是更好的做法,迭代每个解决方案并检查每个学生的答案,或者迭代每个学生并检查每个给定答案的解决方案。可能会有一些性能改进
示例输出:
Enter a DOB:08071994
CorrectMark {1: 2018876, 2: 2017217, 3: 2019374, 4: 2017334, 5: 2018198, 6: 2018962, 7: 2017675, 8: 2018907, 9: 2019666, 10: 2016580}
IncorrectMarks {1: 9160060893760.5, 2: 9165082643626.5, 3: 9158553729255.0, 4: 9164728442565.0, 5: 9162113015853.0, 6: 9159800611514.0, 7: 9163696152020.0, 8: 9159967070664.0, 9: 9157670067978.0, 10: 9167011191702.5}
StudentMarks {1: -9160058874884.5, 2: -9165080626409.5, 3: -9158551709881.0, 4: -9164726425231.0, 5: -9162110997655.0, 6: -9159798592552.0, 7: -9163694134345.0, 8: -9159965051757.0, 9: -9157668048312.0, 10: -9167009175122.5}
Category1 {1: -2291264732790.5, 2: -2290928705099.0, 3: -2289516750015.5, 4: -2291965626589.0, 5: -2290094796904.0, 6: -2291553857417.0, 7: -2290554865412.0, 8: -2288873720765.5, 9: -2287783047122.0, 10: -2292098853947.0}
Category2 {1: -4489931797886.0, 2: -4491550819509.0, 3: -4486677684734.0, 4: -4490156409103.5, 5: -4490175480126.0, 6: -4489135109064.0, 7: -4490128862142.5, 8: -4486618375911.0, 9: -4483367581308.5, 10: -4493394825181.5}
Category3 {1: -6618527592931.0, 2: -6623800004942.5, 3: -6616937789939.5, 4: -6620431491756.0, 5: -6619860293359.5, 6: -6618211160609.5, 7: -6621028446142.0, 8: -6615397046632.0, 9: -6612452385439.5, 10: -6624265848443.5}
Category4 {1: -8267619890383.5, 2: -8273785786215.5, 3: -8265316896835.5, 4: -8270291304668.0, 5: -8268056949093.5, 6: -8266360535811.0, 7: -8270711178658.5, 8: -8264977656482.0, 9: -8263226941953.5, 10: -8274205748901.0}
Category5 {1: -9160058874884.5, 2: -9165080626409.5, 3: -9158551709881.0, 4: -9164726425231.0, 5: -9162110997655.0, 6: -9159798592552.0, 7: -9163694134345.0, 8: -9159965051757.0, 9: -9157668048312.0, 10: -9167009175122.5}