python–生词本

python–生词本背单词是英语中最基础的一环,不少用户在背诵单词的过程中会记录生词,以不断拓展自己的词汇量…classWorld:def__init__(self,world,chinese):self.worl

背单词是英语中最基础的一环,不少用户在背诵单词的过程中会记录生词,以不断拓展自己的词汇量…

class World:
    def __init__(self, world, chinese):
        self.world = world
        self.chinese = chinese

    world = ""
    chinese = ""


class WorldDao:
    # 增加词汇
    def addWorld(self, w):
        a_world = w.world
        a_chinese = w.chinese
        line = a_world + "~" + a_chinese + "\n"
        print(line)
        with open("D:\\world.txt", 'a+') as worldFile:
            worldFile.writelines(line)
        pass

    # 开始背单词
    def showWorld(self):
        print("欢迎打开单词君背诵单词")
        new_worldFile = []
        with open("D:\\world.txt", 'r') as worldFile:
            world_list = worldFile.readlines()
            for index in world_list:
                w = index.split("~")[0]
                c = index.split("~")[1]
                count = 1
                flag = True
                while count < 3 and flag == True:
                    en = input(str(c) + "的汉语意思:")
                    if en == w:
                        print("恭喜答对")
                        flag = False
                    count = count + 1
                flag = True


# 删除单词
def delWorld(self, world):
    goalWorld = input("请输入你要删除的那个单词:")
    temp = []
    with open("D:\\world.txt", 'r') as worldFile:
        list = worldFile.readlines()
        for index in list:
            w = index.split("~")[0]
            if goalWorld is not w:
                temp.append(index)
    pass
    with open("D:\\world.txt", 'w') as file:
        pass

    with open('D:\\world.txt', 'w') as file:
        for i in temp:
            file.writelines(i)
        pass


# 查找单词
def searchWorld(self, world):
    with open("D:\\world.txt", 'r') as worldFile:
        res = worldFile.readlines()
        for index in res:
            w = index.split("~")[0]
            if world == w:
                print(world)
                return World(world, index.split("~")[1])
    pass
    return None


# 清空
def removeAll(self):
    with open("D:\\world.txt", 'w') as file:
        pass


def GoView():
    print("*" * 60)
    print("*", " " * 56, "*")
    print("*", " " * 56, "*")
    print("*", " " * 25, "我的生词本", " " * 21, "*")
    print("*", " " * 56, "*")
    print("*", " " * 56, "*")
    print("*" * 60)


def funView():
    print("*" * 50)
    print("***", " " * 42, "***")

    print("***\t1.加词(1)", " " * 20, "2.背诵(2)\t ***")
    print("***\t3.删除(3)", " " * 20, "4.查找(4)\t ***")
    print("***\t5.清空(5)", " " * 20, "6.退出(6)\t ***")
    print("***", " " * 42, "***")
    print("*" * 50)


GoView()
funView()
type = int(input("请输入你的操作:"))

w = WorldDao()
while type is not 6:
    if type == 1:
        aWorld = input("请输入词汇:")
        aChinese = input("请输入汉语:")
        w.addWorld(World(aWorld, aChinese))
        print("加词成功!")
    elif type == 2:
        w = WorldDao()
        w.showWorld()
    elif type == 3:
        d_World = input("请输入你要删除的单词:")
        w.delWorld(d_World)
    elif type == 4:
        d_World = input("请输入你要查找的单词:")
        flag = w.searchWorld(d_World)
        if flag is None:
            print("没有这个单词")
        else:
            print("单词:", flag.world, " 汉语:", flag.chinese)
    elif type == 5:
        w.removeAll()
    elif type == 6:
        exit(0)
    else:
        print("输入有误,请重新输入!")
    funView()
    type = int(input("请输入你的操作:"))

今天的文章python–生词本分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/61771.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注