<p>希望这是你想要的。有些小错误是因为mcqs.txt文件不可预测的不一致。例如,在问题5中,答案出现了错误。当我们选择容纳问题26的小写答案选择时,我们就把一个邪恶换成另一个邪恶,这就是为什么问题3的答案选择看起来很奇怪(“网站”和“主板”。以“e”和“d”结尾)。同样,问题25写在2\n 5。这就是为什么输出中偶尔会有单独的单词串联在一起。我很想知道原始文件是什么格式的,为什么它会被弄坏?这些是错别字还是你从PDF或其他文件粘贴的?你知道吗</p>
<pre><code>#Get text into workable format
txt=open(r"mcqs.txt","r").readlines()
txt=[line.replace("\n"," ") for line in txt]
txt=[line for line in txt if len(line)>0]
txt=[line.encode('ascii','ignore').decode("utf-8") for line in txt]
txt=[line.strip() for line in txt if line!=" " and line!=""]
txt1="".join(txt)
#Initialize Separator lists
full_test,q_list,let_list=dict(),[str(i)+"." for i in range(1,31)],["A","B","C","D","E","F"]
def segment(txt1,list_of_separators):
#Returns list of tuples
#Tuples define start and end index of separator
i,j,ints,ends=0,0,[],[]
while j<len(list_of_separators):
sep=list_of_separators[j]
if sep in txt1[i:i+len(sep)+1] or sep.lower() in txt1[i:i+len(sep)+1]:
index=i+len(sep)
if txt1[i+len(sep)]==".": index=index+1
ints.append(index)
ends.append(len(sep))
j=j+1
if i==len(txt1):
break
i=i+1
ints=ints+[len(txt1)+ends[-1]]
tups = [(ints[k],ints[k+1]-ends[k]) for k in range(len(ints)-1)]
return tups
#Segment based on question number
tups=segment(txt1,q_list)
#Get blocks of text (includes question and answer choices)
blocks,n=[txt1[tup[0]:tup[1]].strip() for tup in tups],1
for block in blocks:
#Segment based on answer choice
tups=segment(block,[str(i)+"." for i in let_list])
tups=[(0,tups[0][0]-2)]+tups
choices=[block[tup[0]:tup[1]].strip() for tup in tups]
#Initialize dictionary
full_test[n]={"Question":choices[0]}
m=0
for choice in choices[1:]:
full_test[n].update({let_list[m]+".":choice})
m=m+1
n=n+1
#Prompt user for answer as if actually test
for question in full_test.keys():
print(str(question)+"."+full_test[question]["Question"]+"\n")
ind=0
for choice in full_test[question].items():
if ind==0:
ind=ind+1
continue
else:
print(choice[0]+" "+choice[1])
answer=input("\nAnswer:")
full_test[question].update({"Answer":answer})
</code></pre>
<p>如果我对这个挑战没有感觉的话,我会修正格式上的小不一致,并且可能会想出更简单的方法。但有什么好玩的。。。你知道吗</p>