python--生词本

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

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("请输入你的操作:"))

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理想艺术!马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值