用python实现单词本功能

因为笔者最近在准备TOEFL,所以背了好多单词,但是我又觉得百词斩有些低幼了,所以打算自己写一个。

希望实现的功能:
  1. 随机弹出单词的英文。(暂未实现)
  2. 随机弹出单词的中文,要求输入英文并判断是否正确。(已实现)
  3. 输入1弹出下一个单词(中文或英文),输入2弹出对应翻译,输入3结束本次学习,并输出记录
  4. 写入Excel该单词的熟悉度,并根据熟悉度反复检测。
  5. 每天学习的单词不重复(与日期相关)(已实现)。
  6. 图形界面。
Step0:所需工具:
  1. python&python第三方库:time,random,xlrd(安装方法略)
  2. 记录单词的Excel表格
Step1:代码部分(最新版本)
"""
A Harry Chen Code
使用说明:输入1是下一个,2是显示翻译或答案,3是结束学习,4是切换模式。
Setup:将对应的改名字,我的默认是
0:单词  1:分类  2:目录(单词书上的位置)     3:翻译
"""

import xlrd
import time
day = time.strftime("%d")
data = xlrd.open_workbook(r'C:\Users\...\vocabulary.xlsx')
table = data.sheet_by_name('TOTAL')
my_nrows = table.nrows
counts = 0
mode = 1
scope = int(my_nrows / 30)
today = int(day) * scope
the_chosen_word = today             # this is for efficient mode


def show_en(word_num):
    print("\t\t", table.cell_value(word_num, 0), '   ',
          table.cell_value(word_num, 1), '   ',
          table.cell_value(word_num, 2))  # 0:单词  1:分类  2:目录


def show_cn(word_num):
    print("\t\t Translation", table.cell_value(word_num, 3))


def control1(str_a, word_num):
    global counts
    if str_a == str(1):
        show_en(word_num)
        counts += 1
        return 1
    elif str_a == str(2):
        show_cn(word_num - 1)
        return 1
    elif str_a == str(3):       # break
        return 3
    elif str_a == str(4):
        print("已切换模式")
        return 2  # means switch mode
    else:
        print("1:下一个    2:显示答案      3:结束学习       4:切换模式")
        return 1


def control2(str_a, word_num):
    global counts
    if str_a == str(1):
        print('\t\t请翻译:' + table.cell_value(word_num, 3))
        type_in = input()
        if type_in == table.cell_value(word_num, 0):
            print("\t\tCorrect!")
        else:
            show_en(word_num)
        counts += 1
        return 2
    elif str_a == str(3):
        return 3
    elif str_a == str(4):
        print("已切换模式")
        return 1  # means switch mode
    else:
        print("1:下一个      3:结束学习       4:切换模式")
        return 2


while mode != 3:
    a = input("输入指令:")
    if mode == 1:
        mode = control1(a, the_chosen_word)
        if a == str(1):
            the_chosen_word += 1

    if mode == 2:
        mode = control2(a, the_chosen_word)
        if a == str(1):
            the_chosen_word += 1

    if the_chosen_word == today + 30:  # this is for efficient mode
        the_chosen_word = today
        print("Done a roll!")

print("本次复习单词{}个".format(counts))
不足之处:

因为每次都需要重新获取翻译,所以放慢了速度,下次可以加入xlwt库在单词旁边写入翻译,而且translator其实并不是很可靠,还需要连上网才可以。(已解决)
很遗憾的删除了随机功能~,等我暑假的时候在完善吧。

笔记:
  • 在最近的一次更新中,我再也忍不了translator这个垃圾第三方库了,所以我找到了替代品,在Excel里用一个函数(有道的接口)
=FILTERXML(WEBSERVICE("https://fanyi.youdao.com/translate?&i="&A1&"&doctype=xml"),"//translation")
  • 当Excel打开的时候,程序运行会报错(也许是因为我把表格文件放在OneDrive上)
更新记录
  • V1.01: 完成基本要求(功能3&5)。
  • V1.1: 改进翻译功能,完成功能2。
  • V2.0: 重写了结构,暂时删除了随机功能,添加了效率功能。