回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有一个文本文件,其中有30个选择题在下面的模式</p>
<ol>
<li><p>第一个问题在这里?你知道吗</p>
<p>A.方案1</p>
<p>B.方案2</p>
<p>C.方案3</p>
<p>D.选项4</p></li>
</ol>
<p>以此类推到30岁</p>
<p>选项的数量是可变的;至少有两个选项,最多有六个选项。你知道吗</p>
<p>我想在html/php测验这样的界面上练习这些问题,它允许我选择选项并最终显示结果。你知道吗</p>
<p>我试着用python阅读这个文件,然后尝试将问题和答案存储在单独的列表中,但没有成功。
下面是我的代码:</p>
<pre><code>#to prevent IndexError
question = ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
answers = ['','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','']
qOrA = "q"
mcq_file = "mcqs.txt"
mcq = open(mcq_file, "r")
data_list = mcq.readlines()
for i in range(len(data_list)):
element = list(data_list[i])
if element[0] == "A" and element[1] == ".":
qOrA = "a"
if qOrA == "q":
question[i] = question[i]+ " " + data_list[i]
elif qOrA == "a":
answers[i] = answers[i]+ " " + data_list[i]
</code></pre>
<p>你知道吗mcq.readlines文件()问题3的结果如下
注意:实际上有多个换行符,因此文件结构不正确。你知道吗</p>
<pre><code>['\n', '1.\n', '\n', ' \n', '\n', 'Which computer component contains all the \n', '\n', 'circuitry necessary for all components or \n', '\n', 'devices to communicate with each other?\n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', 'A. Motherboard\n', '\n', ' \n', '\n', ' \n', '\n', 'B. Hard Drive\n', '\n', ' \n', '\n', ' \n', '\n', 'C. Expansion Bus\n', '\n', ' \n', '\n', ' \n', '\n', 'D. Adapter Card\n', '\n', ' \n', '\n', ' \n', '\n', '\n', '\n', '\n', '2. \n', '\n', 'Which case type is typically \n', '\n', 'used for servers?\n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', 'A.\n', '\n', ' \n', '\n', ' \n', '\n', 'Mini Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'B.\n', '\n', ' \n', '\n', ' \n', '\n', 'Mid Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'C.\n', '\n', ' \n', '\n', ' \n', '\n', 'Full Tower\n', '\n', ' \n', '\n', ' \n', '\n', 'D.\n', '\n', ' \n', '\n', ' \n', '\n', 'desktop\n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', '\n', '\n', '\n', '3.\n', '\n', ' \n', '\n', 'What is the most reliable way for users to buy the \n', '\n', 'correct RAM to upgrade a computer?\n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', 'A.\n', '\n', ' \n', '\n', ' \n', '\n', 'Buy RAM that is the same color as the memory sockets \n', '\n', 'on the motherboard.\n', '\n', ' \n', '\n', ' \n', '\n', 'B.\n', '\n', ' \n', '\n', ' \n', '\n', 'Ensure that the RAM chip is the same size as the ROM chip.\n', '\n', ' \n', '\n', ' \n', '\n', 'C.\n', '\n', ' \n', '\n', ' \n', '\n', 'Ensure that the RAM is \n', '\n', 'compatible\n', '\n', ' \n', '\n', 'with the peripherals \n', '\n', 'installed on the motherboard.\n', '\n', ' \n', '\n', ' \n', '\n', 'D.\n', '\n', ' \n', '\n', ' \n', '\n', 'Check the motherboard manual or manufacturer’s website.\n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', ' \n', '\n', '\n', '\n', '\n']
</code></pre>