Python背单词记单词小程序
时间: 2023-11-16 13:12:33 浏览: 209
好的,以下是一个简单的背单词记单词小程序的实现:
```python
import random
# 单词类
class Word:
def __init__(self, word, meaning):
self.word = word
self.meaning = meaning
# 词库类
class Vocabulary:
def __init__(self):
self.words = []
# 添加单词
def add_word(self, word, meaning):
self.words.append(Word(word, meaning))
# 背单词程序类
class WordLearning:
def __init__(self, vocabulary):
self.vocabulary = vocabulary
self.current_word = None
self.score = 0
self.wrong_words = []
# 随机选择一个单词
def get_word(self):
self.current_word = random.choice(self.vocabulary.words)
# 展示单词并获取用户输入
def show_word(self):
print("单词:", self.current_word.word)
answer = input("请输入单词的意思:")
return answer
# 检查用户输入的答案是否正确
def check_answer(self, answer):
if answer == self.current_word.meaning:
self.score += 1
print("回答正确!")
else:
self.wrong_words.append(self.current_word)
print("回答错误!正确答案为:", self.current_word.meaning)
# 展示得分情况
def show_score(self):
print("本次得分:", self.score)
# 展示错词并重新学习
def review_wrong_words(self):
if self.wrong_words:
print("以下是你错过的单词:")
for word in self.wrong_words:
print(word.word, word.meaning)
print("重新学习!")
self.wrong_words = []
self.score = 0
self.start_learning()
else:
print("没有错过的单词!")
# 开始学习
def start_learning(self):
print("开始学习!")
while True:
self.get_word()
answer = self.show_word()
self.check_answer(answer)
another_word = input("是否继续?(y/n)")
if another_word.lower() == "n":
break
self.show_score()
self.review_wrong_words()
# 示例
vocabulary = Vocabulary()
vocabulary.add_word("apple", "苹果")
vocabulary.add_word("banana", "香蕉")
vocabulary.add_word("cherry", "樱桃")
vocabulary.add_word("orange", "橙子")
vocabulary.add_word("pear", "梨子")
word_learning = WordLearning(vocabulary)
word_learning.start_learning()
```
这个程序很简单,只有一个学习模式,用户输入单词的意思,程序判断是否正确,最后展示得分情况和错词,并可以重新学习错词。如果需要更多的学习模式和功能,可以根据需要进行扩展。
阅读全文