生成试题截图效果
-
在本地硬盘建立一个源试题程序文件 -
-
利用下面程序代码,导入txt文件,同时生成一批练习文件 -
这个试题随机生成的效果还是很棒的!
功能更新:
- 原程序,只能输出一组试题,但问题是每题答案不同,有的单词首字母是大小,而有的单词首字母是小写的,这就需要我们根据题目的设置,修改一下答案中的单词首字母
- 于是对程序进行了一个if…elif…的调整,根据答案先判断这个题目的正确答案是首字母大写,还是首字母小写,如果是大写,全部答案改为大写,如果是小写,那全部改变小写。
letter_dx = "False" if ord('a') <= ord(correctAnswer[0]) <= ord('z') else "True"
if letter_dx == 'True':
answerOptions_dx = [i.capitalize() for i in answerOptions]
for i in range(4):
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions_dx[i]))
elif letter_dx == 'False':
for i in range(4):
answerOptions_dx = [i.lower() for i in answerOptions]
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions_dx[i]))
import random,os
d = {}
name = ''
def file_list():
files = os.listdir('F:\物理练习图\九年级\字典组建试题')
return files
file = file_list()
def english(file):
for ind,fil in enumerate(file):
print(ind,'---',fil)
for ind,fil in enumerate(file):
fil = ''.join(fil)
xz = input('选择序号:')
if fil.endswith('.txt'):
if xz == ind:
fil = fil
with open(rf"F:\物理练习图\九年级\字典组建试题\{fil}") as f:
print(f'{fil}试题组建成功---F:\物理练习图\九年级\英语试题生成文件 ')
for line in f:
key,val = line.split('|')
val = ''.join(val)
val = val.strip('\n')
d[key] = val
return d
eng_tests = english(file)
pd_letter_dx = []
file_name = input('文件名称:')
for quizNum in range(20):
quizFile = open(r'F:\物理练习图\九年级\英语试题生成文件\{}-{}.doc'.format(file_name,quizNum + 1),'w',encoding='utf-8')
answerKeyFile = open(r'F:\物理练习图\九年级\英语试题生成文件\{}-_答案{}.txt'.format(file_name,quizNum + 1), 'w')
quizFile.write('姓名:\t\t日期:\t\t学期:\t\t')
quizFile.write((' ' * 20) + '英语专题测试 (卷 %s)' % (quizNum + 1))
quizFile.write('\n\n')
eng_test_list = list(eng_tests.keys())
random.shuffle(eng_test_list)
for questionNum in range(len(eng_test_list)):
correctAnswer = eng_tests[eng_test_list[questionNum]]
print(correctAnswer,'正确答案')
wrongAnsewers = list(eng_tests.values())
del wrongAnsewers[wrongAnsewers.index(correctAnswer)]
wrongAnsewers = random.sample(wrongAnsewers, 3)
answerOptions = wrongAnsewers + [correctAnswer]
random.shuffle(answerOptions)
quizFile.write('%s. %s \n' %(questionNum + 1,
eng_test_list[questionNum]))
letter_dx = "False" if ord('a') <= ord(correctAnswer[0]) <= ord('z') else "True"
if letter_dx == 'True':
answerOptions_dx = [i.capitalize() for i in answerOptions]
for i in range(4):
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions_dx[i]))
elif letter_dx == 'False':
for i in range(4):
answerOptions_dx = [i.lower() for i in answerOptions]
quizFile.write(' %s. %s\n' % ('ABCD'[i], answerOptions_dx[i]))
quizFile.write('\n')
answerKeyFile.write('%s.%s\n' %(questionNum + 1, 'ABCD'[
answerOptions.index(correctAnswer)]))
quizFile.close()
answerKeyFile.close()
希望更多的老师借鉴,提出宝贵的建议!
|