import random
D = {
'uw': 'opportunity chance。senior citizens old-people。influence, impact effect', # 示例1:英语词汇升级
'zc': '度 一夜飞()镜湖月,猿猱欲()愁攀援。洲、渡 楼船夜雪瓜()()。箫 乘醉听()鼓,凤()声动', # 示例2:名篇名句默写易混字词辨析
}
t1 = ['uw'] # 示例:属于type1的背诵条目
nameL = input('name(sep with ","): ').split(',')
sep = input('type if not separate: ')
sentL1 = []
sentL = []
type1 = False
type2 = False
for name in nameL:
sentL1 += D[name].split('。')
if name in t1:
type1 = True
else:
type2 = True
if type1 == True and type2 == False:
for sent1 in sentL1:
sent1L = sent1.split()
sent1Leng = ' '.join(sent1L[:-1])
sent1Lchi = sent1L[-1]
sentL.append([sent1Lchi, sent1Leng])
elif type1 == False and type2 == True:
for sent1 in sentL1:
sent1L = sent1.split()
sent1Leng = sent1L[0]
if sep == '':
sent1LchiL = ' '.join(sent1L[1:]).split(',')
for sent1Lchi in sent1LchiL:
sentL.append([sent1Lchi, sent1Leng])
else:
sent1Lchi = ' '.join(sent1L[1:])
sentL.append([sent1Lchi, sent1Leng])
else:
raise TypeError('You cannot use both types at a time.')
num = len(sentL)
print('total number: ' + str(num))
random.shuffle(sentL)
for sent in sentL:
input(str(num) + '>>> ' + sent[0])
print(sent[1])
num -= 1
该程序的工作模式是这样的:把问答写入程序,而后程序自动将各个问答随机排序,显示问题,用户需要思考出回答然后按回车键才能看见答案,进行比对。例如,这样的英语词汇升级背诵材料就是适合的问答内容:
chance -> oppportunity
old people -> senior citizens
effect -> influence, impact
其中,箭头左侧为“问题”(即提示语),右侧为“答案”。或者这样的名篇名句默写易混易错字词辨析:
一夜飞()镜湖月 -> 度
猿猱欲()愁攀援 -> 度
楼船夜雪瓜()() -> 洲、渡
乘醉听()鼓 -> 箫
凤()声动 -> 箫
因此,问答材料可以大致分为两类:其一,以如上第一段材料为代表的,“问题”和“答案”一一对应(即一般情况下各个问题的答案互不相同),并且答案部分的空格较多;其二,以如上第二段材料为代表的,答案中不含空格,并且可能存在问题与答案并非一一对应的情况。在第一类中,答案允许出现空格增大了其自由度,而第二类中多个问题共同指向一个答案的情况明显可以合并。因此,不同的材料就被分为Type1和Type2两类,分别对症下药。
需要背诵的材料储存在如下名为“D”的字典中:
D = {
'uw': 'opportunity chance。senior citizens old-people。influence, impact effect', # 示例1:英语词汇升级
'zc': '度 一夜飞()镜湖月,猿猱欲()愁攀援。洲、渡 楼船夜雪瓜()()。箫 乘醉听()鼓,凤()声动', # 示例2:名篇名句默写易混字词辨析
}
材料输入的格式要求在上一篇博文《Python辅助高效背诵记忆知识点(零基础教程,手机版可用)》中有具体讲解。
首先向用户询问背诵材料的名称:
nameL = input('name(sep with ","): ').split(',')
本程序支持同时背诵多则材料,只需用英文逗号隔开不同名称,但是限于两种类型的背诵材料后续处理方式不同,不能将两者混合背诵。故设置了以下防止出错的机制:
t1 = ['uw'] # 示例:属于type1的背诵条目
sentL1 = []
sentL = []
type1 = False
type2 = False
for name in nameL:
sentL1 += D[name].split('。')
if name in t1:
type1 = True
else:
type2 = True
if type1 == True and type2 == False:
pass
elif type1 == False and type2 == True:
pass
else:
raise TypeError('You cannot use both types at a time.')
这段程序同时还做到了将材料中的问题-答案组以中文句号为间隔符分隔开,并以问题-答案组为元素储存在列表sentL1中。
与此同时,Type2的材料又可分为两种:一种是多个问题对应一个答案的模式,另一种是问题与答案一一对应,且不需要把中文句号识别为分隔符的模式。因此,需要提问是否需要分隔:
sep = input('type if not separate: ')
对于Type1的材料,需要将问题-答案组(即sentL1中的元素sent1)中最后一个空格作为答案与问题分隔符,而忽略答案本身的一切空格。最后,将问题与答案作为一个二元列表插入列表sentL中。
if type1 == True and type2 == False:
for sent1 in sentL1:
sent1L = sent1.split()
sent1Leng = ' '.join(sent1L[:-1])
sent1Lchi = sent1L[-1]
sentL.append([sent1Lchi, sent1Leng])
其中,sent1Leng指答案,sent1Lchi指问题。
对于Type2的材料,同样先把所有空格作为分隔符分开,转化成一个列表,由于答案本身不含空格,故列表的第一个元素即为答案。对于问题,需要根据sep的回答进行不同处理:假如sep回答为空字符串,意味着需要以中文逗号为分隔符将问题分开;否则忽略中文逗号。
elif type1 == False and type2 == True:
for sent1 in sentL1:
sent1L = sent1.split()
sent1Leng = sent1L[0]
if sep == '':
sent1LchiL = ' '.join(sent1L[1:]).split(',')
for sent1Lchi in sent1LchiL:
sentL.append([sent1Lchi, sent1Leng])
else:
sent1Lchi = ' '.join(sent1L[1:])
sentL.append([sent1Lchi, sent1Leng])
为了打乱答案-问题组,需要引入random模块,并使用shuffle函数打乱列表。
import random
random.shuffle(sentL)
首先,需要告诉用户背诵材料中答案-问题组的总数:
num = len(sentL)
print('total number: ' + str(num))
其次,只需把答案-问题组一个一个呈现出来,每次呈现时同时给出剩余的组数:
for sent in sentL:
input(str(num) + '>>> ' + sent[0])
print(sent[1])
num -= 1