GameMale
登陆 / 注册 搜索

USERCENTER

SEARCHSITE

搜索

查看: 1583|回复: 19
收起左侧

[实用工具] 【python】简单的背单词程序

[复制链接] |关注本帖

邪恶的面具海盗弯钩神秘的红茶冒险用绷带魔法石碑箭术卷轴质量效应三部曲

     楼主| 白冥 发表于 2024-7-9 20:41:21 | 显示全部楼层 |阅读模式 <
    本帖最后由 白冥 于 2024-7-9 21:06 编辑

    最近在学日语,因此做了一个简单的背单词程序,允许你写入单词表来背诵。单词对象,包括单词的拼写、含义、进度(初始为0)、是否已完成记忆(初始为False)、是否是难词(初始为False)以及尝试次数(初始为0)。如果填对拼写,则增加单词的记忆进度。如果进度达到100,标记为已完成。否则填错或者不填,减少单词的记忆进度。如果尝试次数达到30次,标记为难词。
    这个程序会
    初始化一个空的单词列表,允许用户输入单词的拼写和含义,然后创建一个新的 Word 对象并添加到列表中,然后从单词列表中随机选择指定数量的单词,如果请求的单词数量大于列表中的单词数量,则返回列表中的所有单词。
    程序执行时,会调用 recite_words 函数,并尝试记忆50个单词,这个函数首先创建一个 WordList 对象,并更新单词列表,然后,它随机选择指定数量的单词进行记忆。对于每个未完成的单词,程序会显示单词的含义,并要求用户输入单词的拼写。如果用户输入正确,单词的记忆进度会增加;如果输入错误,进度会减少,并且如果尝试次数过多,单词会被标记为难词,最后,所有被标记为难词的单词会被写入到 "hard_word_list.txt" 文件中。


    ————————————

    import random
    class Word:
        def __init__(self, spelling, meaning):
            self.spelling = spelling
            self.meaning = meaning
            self.progress = 0
            self.haveCompleted = False
            self.isHardWord = False
            self.count = 0
        def addProgress(self):
            if self.progress < 100:
                self.progress += 1
            elif self.progress >= 100:
                self.haveCompleted = True
        def subProgress(self):
            if self.progress > 0:
                self.progress -=1
            self.count += 1
            if self.count >= 30:
                self.isHardWord =True
    class WordList:
        def __init__(self):
            self._word_list = []
        def updata_word_list(self):
            print("Updata the word list now...")
            self._word_list.clear()
            while(True):
                    spelling = input("Enter next word's spelling")
                    if not spelling:
                        break
                    meaning = input("Enter the word's meaning")
                    word = Word(spelling,meaning)
                    self._word_list.append(word)
        def select_random_word(self,count):
            return random.sample(self._word_list, min(count,len(self._word_list)))
    def recite_words(count):
        recite_words_program = WordList()
        recite_words_program.updata_word_list()
        word_list=recite_words_program.select_random_word(count)
        if not word_list:
            raise Exception("You can't try to recite an empty word list!")
        hard_word_list = []
        while(True):
            if all(word.haveCompleted for word in word_list):
                break
            word = random.choice([word for word in word_list if not word.haveCompleted])
            print(word.meaning)
            if input("The word's spelling is...") == word.spelling:
                word.addProgress()
            else:
                word.subProgress()
            if word.isHardWord:
                hard_word_list.append(word.spelling)
        with open("hard_word_list.txt","a") as f:
            f.write("This is the hard words of the feedback:")
            i = 0
            for word in hard_word_list:
                f.write(f"{i}:  {word}\n")
                i += 1
    if __name__ == "__main__":
        recite_words(50)
    ————————————


    评分

    参与人数 2血液 +7 追随 +1 堕落 +1 收起 理由
    书の妖怪 + 3 + 1 很给力!
    bigbigbig3 + 4 + 1

    查看全部评分

      收起(2)
    • 白冥 白冥 :觉得好用就给一个免费的追随吧!
      2024-07-09 21:04 回复
    • 白冥 白冥 :优化了程序,现在错得越多的单词越容易被选中,概率权重与ln(word.count)有关:
      import random
      import math
      class Word:
          def __init__(self, spelling, meaning):
              self.spelling = spelling
              self.meaning = meaning
              self.progress = 0
              self.haveCompleted = False
              self.isHardWord = False
              self.count = 0
          def addProgress(self):
              if self.progress < 100:
                  self.progress += 1
              elif self.progress >= 100:
                  self.haveCompleted = True
          def subProgress(self):
              if self.progress > 0:
                  self.progress -=1
              self.count += 1
              if self.count >= 30:
                  self.isHardWord =True
      class WordList:
          def __init__(self):
              self._word_list = []
          def updata_word_list(self):
              print(\"Updata the word list now...\")
              self._word_list.clear()
              while(True):
                      spelling = input(\"Enter next word\'s spelling\")
                      if not spelling:
                          break
                      meaning = input(\"Enter the word\'s meaning\")
                      word = Word(spelling,meaning)
                      self._word_list.append(word)
          def select_random_word(self,count):
              return random.sample(self._word_list, min(count,len(self._word_list)))
      def recite_words(count):
          recite_words_program = WordList()
          recite_words_program.updata_word_list()
          word_list=recite_words_program.select_random_word(count)
          if not word_list:
              raise Exception(\"You can\'t try to recite an empty word list!\")
          hard_word_list = []
          while(True):
              if all(word.haveCompleted for word in word_list):
                  break
              current_list = [word for word in word_list if not word.haveCompleted]
              weights = [math.log(word.count + 1) for word in current_list]
              total_weight = sum(weights)
              probabilities = [weight/total_weight for weight in weights]
              word = random.choices(current_list, weights=probabilities, k=min(count, len(current_list)))
              print(word.meaning)
              if input(\"The word\'s spelling is...\") == word.spelling:
                  word.addProgress()
              else:
                  word.subProgress()
              print(f\"spelling:{self.spelling}\\nmeaning:{self.meaning}\\n\")
              if word.isHardWord:
                  hard_word_list.append(word.spelling)
          with open(\"hard_word_list.txt\",\"a\") as f:
              f.write(\"This is the hard words of the feedback:\")
              i = 0
              for word in hard_word_list:
                  f.write(f\"{i}:  {word}\\n\")
                  i += 1
      if __name__ == \"__main__\":
          recite_words(50)
      2024-07-09 21:47 回复
    • 我也说一句

    回复

    使用道具 举报

    漂洋小船冒险用指南针破损的旧书人鱼之泪雪王的心脏苏格兰圆脸胖鸡[Pro Max]丛林的鸟飞走了不曾寄出的信件幽灵竹筒『随时随地开启!』

      凯诺斯 发表于 2024-7-9 20:56:58 | 显示全部楼层 <
      看起来很实用的背单词程序惹,试试看效果如何
        收起(4)
      回复

      使用道具 举报

      这是怎么辉石呢吃了一半的面包死亡化身BIG BOSS实现梦想官复原职召唤兽拉姆寶可夢 Pokémon双重身份擎天柱(Peterbilt389)

        糖醋鲤鱼 发表于 2024-7-9 20:59:38 | 显示全部楼层 <
        哈哈哈,看起来这个好有趣的样子呢,改天可以试一下,刚好我最近也在学日语呢
        回复

        使用道具 举报

        弗雷迪玩偶发条八音盒桂花米糕鎏彩万幢女巫之路虚空之海的鲸【新春限定】果体 隆『召唤好运的角笛』永远的克叔業火死鬥

          娱乐法师火布偶 发表于 2024-7-9 21:08:50 | 显示全部楼层 <
          回复

          使用道具 举报

          遍江云霞人鱼之泪苍穹禁城星芒戒指岛田源氏岛田半藏“腐败女神”玛莲妮亚女巫之路传说全景

            Protoss 发表于 2024-7-9 22:03:49 | 显示全部楼层 <
            回复

            使用道具 举报

            美恐:启程鎏彩万幢守卫: 轮班侦察『林中过夜』『厢庭望远』金钱马车巴哈姆特『狄文卡德的残羽』禽兽扒手骑兽之子

              li漠北 发表于 2024-7-9 22:45:48 | 显示全部楼层 <
              会写程序的技能真的好加分,符合对理工男的任何想法
              回复

              使用道具 举报

              我的天使GM吸血伯爵吃饱金币的Doge苏格兰圆脸胖鸡小小舞台守卫: 坚守眼位永浴爱河肉垫手套御医神兔『搓粉团珠』

                毛茸茸兽兽 发表于 2024-7-9 23:39:17 | 显示全部楼层 <
                瑟瑟发抖ing(´×ω×`)想起了当初死记单词的时间哩,咱用的是不背单词,有些功能还类似的咧~
                回复

                使用道具 举报

                缘起星空虚空之海的鲸

                  you9632587 发表于 2024-7-10 07:43:29 | 显示全部楼层 <
                  用程序来背单词怕是摸上电脑就开始不自觉干起别的了吧
                    收起(2)
                  回复

                  使用道具 举报

                  组成家庭

                    ditto 发表于 2024-7-10 09:41:37 | 显示全部楼层 <
                    回复

                    使用道具 举报

                    冰海钓竿史莱姆牧场吸血魔蝠萨菲罗斯苏醒的格罗姆圣甲虫秘典可爱黑猫诺克提斯·路西斯·伽拉姆风暴磁场之鳄業火死鬥

                      圣卫幻梦 发表于 2024-7-10 10:12:20 | 显示全部楼层 <
                      挺有趣的,不过这类型的东西还得是便携端的好用一些
                      回复

                      使用道具 举报

                      赛博朋克2077剑指撒冷『冰雕马拉橇』【圣诞限定】心心念念小雪人官复原职岛田源氏里昂‧S‧甘乃迪擎天柱(Peterbilt389)

                        枫_Age 发表于 2024-7-10 10:46:52 | 显示全部楼层 <
                        如果我是高中生我肯定不屑一顾,但是大学生真的很需要这个,谢谢orz
                        回复

                        使用道具 举报

                        GM論壇榮譽勛章信仰之心『樱花树灵』风物长宜近地夜航鎏彩万幢SCP-s-1889-第五页巴哈姆特

                          maplez 发表于 2024-7-10 14:09:08 | 显示全部楼层 <
                          感觉过于专业了   可能对于普通人还是成品app的功能就足够了
                          回复

                          使用道具 举报

                          暮狼归来黑暗交易炼金之心雷霆晶球敖蜃星发条八音盒弗雷迪玩偶卡洛斯·奥利维拉里昂‧S‧甘乃迪秘法之书

                            95269527 发表于 2024-7-10 19:08:08 | 显示全部楼层 <
                            回复

                            使用道具 举报

                            虚空之海的鲸『钜鲸』守卫: 坚守眼位御医神兔哀恸魔蝶镜中小鸟孔明灯菀叶狸猫猪庇特小狮欢舞

                              cdcai 发表于 2024-7-10 19:17:43 | 显示全部楼层 <
                              回复

                              使用道具 举报

                              普隆普特·阿金塔姆龙腾世纪:审判琉璃玉坠力量腕带冒险专用绳索擎天柱(Peterbilt389)威克多尔·克鲁姆诺克提斯·路西斯·伽拉姆

                                Linkdahl 发表于 2024-7-10 19:30:34 | 显示全部楼层 <
                                好简短的python脚本,简短素程序员的浪漫惹,不过本可已经没有学习语言的动力了,还是只能鼓励一下lz惹
                                  收起(3)
                                回复

                                使用道具 举报

                                安德鲁·库珀索尔·奥丁森安德森‧戴维斯走出失恋阴影的罗罗詹米·多南

                                  bigbigbig3 发表于 2024-7-10 21:04:28 | 显示全部楼层 <
                                  背词也讲求科学方法额
                                  好厉害的工具哈哈哈
                                  可以顶过那些花里胡哨的软件了
                                    收起(3)
                                  回复

                                  使用道具 举报

                                  『住在GM村』神奇宝贝图鉴神奇宝贝大师球万众瞩目『搓粉团珠』『厢庭望远』『狄文卡德的残羽』检定场命运的轮廓雪王的心脏

                                    zhuovboyan 发表于 2024-7-11 04:29:11 | 显示全部楼层 <
                                    回复

                                    使用道具 举报

                                    寻觅山猫图腾眼镜蛇图腾特殊-家园卫士Ⓛ亚当‧简森GM論壇榮譽勛章『狄文卡德的残羽』

                                      danst110 发表于 2024-7-11 11:39:34 | 显示全部楼层 <
                                      回复

                                      使用道具 举报

                                      卡利亚权杖诺克史黛拉之月尾崎八项双向圣杯:焕然意志缘起星空史莱姆牧场

                                        ZHD 发表于 2024-7-17 21:28:58 | 显示全部楼层 <
                                        回复

                                        使用道具 举报

                                        脉律辐石劫掠核芯御医神兔夏日柯基幽灵竹筒黄金树的恩惠探险三杰士图腾饼干生活拍立得璀璨金币

                                          chouki 发表于 2024-7-26 21:07:19 | 显示全部楼层 <
                                          回复

                                          使用道具 举报

                                          您需要登录后才可以回帖 登录 | 立即注册

                                          本版积分规则

                                          关闭

                                          站长公告上一条 /1 下一条

                                          文字版|手机版|小黑屋|GameMale

                                          GMT+8, 2025-12-8 07:37 , Processed in 0.180813 second(s), 145 queries , Redis On.

                                          Copyright © 2013-2025 GameMale

                                          All Rights Reserved.

                                          快速回复 返回列表