|
本帖最后由 白冥 于 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)
————————————
|
评分
-
查看全部评分
- 白冥
:觉得好用就给一个免费的追随吧!
- 白冥
:优化了程序,现在错得越多的单词越容易被选中,概率权重与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)
-
|