Python 简易单词拼写背诵代码

发布时间:2023年12月26日

需要一个TXT文档,自己敲呗,谁叫我是无限猴子悖论中的一员呢~

district,地区,区域
agent,代理人,代理商,经纪人
fiction,小说,虚构的事
yield,屈服让步,产出,产量
bother,使烦恼,打扰,麻烦
tragedy,不幸,灾难,悲剧作品
gadget,小器具,小玩具
orient,使适应熟悉,使确立方位,东方,为··专门设计
episode,插曲,片段,一集,一节
remedy,纠正方法,补救方法,药品,治疗方法
pose,产生,造成,冒充假装,摆出姿势,姿态
prohibit,禁止不允许,阻止,使不可能

这是代码:

输入0,可以选范围

答题中

0为保存退出

-1为直接退出,不计入错题集

回车为下一个,不计入错题集

import sys
import re
import time
from random import randint, sample

wo = []
err = {}
kkk = {"剩余单词:": 0, "错误单词:": 0, "错误上限:": 0, "答题用时:": 0, "累计用时:": 0}
h = 1
b = -1 - h
filename = "26-30words.txt"
a = open(sys.path[0] + "/" + filename, mode='r', encoding='utf-8')
a.seek(0)
r = re.compile(r'^[a-zA-Z]')
for line in a:
    first = ''
    end = ""
    if line == "\n":
        continue
    for ii in list(line):  #
        ii = str(ii)
        if r.match(ii):
            first = first + str(ii)
        else:
            end = end + str(ii)
    wo.append([first, end, -1, 0])
a.close()
#
fname = f"{filename.split('.')[0]}错题集.txt"  # 保存名称
j = open(sys.path[0] + "/" + fname, "a+",encoding='utf-8')
j.seek(0)
for line in j:  # 文件读取
    for h in kkk.keys():
        if line.find(h) != -1:
            kkk[h] = line[5:]
j.close()
print("上次答题情况:\a")
for i, j in kkk.items():
    print(i, j, end="")
t4 = int(kkk["累计用时:"])
#
err = {}


def add(a, b):  # 关键函数
    if a[2] == 0:
        word.remove(a)
    elif a[2] <= b:
        err[str(a[0]) + str(a[1])] = a[3]
        print(f"已错误{a[3]}次\n", str(a[0]) + str(a[1]))


print("总单词量----->", len(wo))
#########################
print(f"退出输入----->-1\n结束输入------>0\n错{-1 - b}次会被记录在文件中")
#########################题库选择
mm = input("挑战词量=")
word = []
if not mm.isdigit() or mm == "":
    mm = len(wo)
    print(f"全部词量{mm}")
    word = sample(wo, mm)
elif mm == "0":
    try:
        mma = int(input("开始点="))
        mmb = int(input("结束点="))
        word = wo[mma - 1:mmb]
        mm = len(word)
    except Exception as err:
        print(err)
        quit()
    print(len(word))
else:
    try:
        mm = int(mm)
        word = sample(wo, mm)
    except Exception as err:
        print(err)
        quit()
# 核心作答模块
t1 = time.time()
while len(word) >= 1:
    r = randint(0, len(word) - 1)
    s = word[r]
    print(len(word), s[1], "")
    c = input(s[0][0:2] + (len(s[0]) - 2) * "-" + "\r")
    if c == s[0]:
        word[r][2] += 1
        print("答对了~~")
    elif c == "0":
        break
    elif c == "-1":
        quit()
    elif c == "":
        continue
    else:
        print(s[0], "SB!")
        word[r][2] -= 1  # 从列表中去除的可能性越来越小
        word[r][3] += 1  # 累计答错次数增加
    add(word[r], b)
    print(r)
# 累计时间模块
t2 = time.time()
t3 = str(int((t2 - t1) / 60)) + "min"
t5 = t4 + int((t2 - t1) / 60)
# 作答结果保存模块
j = open(sys.path[0] + "/" + fname, "a+",encoding='utf-8')
j.seek(0)
j.write("\n-----------" + time.strftime('%Y-%m-%d %H:%M:%S') + "---------------\n")
kkk = {"剩余单词:": str(len(word)) + "/" + str(mm), "错误单词:": len(err), "错误上限:": h, "答题用时:": t3,
       "累计用时:": t5}
outcome = []  # 结果参数保存
for m, n in kkk.items():
    j.write(m + str(n) + "\n")
    outcome.append(m + str(n) + "\n")
outcome2 = []
for a, b in err.items():  # 把错误单词和错误次数写到文件里
    outcome2.append("{: <4}".format(str(b)) + str(a))
outcome2.sort()
for g in outcome2:
    j.write(g)
j.close()
for x in outcome:
    print(x, end="")

这个里面的问题还有很多,我有时间更~

祝你背的愉快!

文章来源:https://blog.csdn.net/2301_80023082/article/details/135172188
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。