欢迎访问宙启技术站
智能推送

在Python中实现一个Vocabulary()类,用于单词的学习和记忆

发布时间:2023-12-25 01:42:54

以下是一个简单的实现Vocabulary()类的例子:

class Vocabulary:
    def __init__(self):
        self.words = {}
    
    def add_word(self, word, meaning):
        self.words[word] = meaning
    
    def display_word(self, word):
        if word in self.words:
            print(f"{word}: {self.words[word]}")
        else:
            print("Word not found in vocabulary.")
    
    def delete_word(self, word):
        if word in self.words:
            del self.words[word]
            print(f"{word} deleted from vocabulary.")
        else:
            print("Word not found in vocabulary.")
    
    def update_meaning(self, word, new_meaning):
        if word in self.words:
            self.words[word] = new_meaning
            print(f"Meaning of {word} updated.")
        else:
            print("Word not found in vocabulary.")
    
    def learn_word(self):
        word = input("Enter the word to learn: ")
        meaning = input("Enter the meaning of the word: ")
        self.add_word(word, meaning)
        print(f"{word} added to vocabulary.")
    
    def memorize_word(self):
        word = input("Enter the word to memorize: ")
        self.display_word(word)
    
    def menu(self):
        while True:
            print("1. Add word to vocabulary")
            print("2. Display word meaning")
            print("3. Delete word from vocabulary")
            print("4. Update word meaning")
            print("5. Learn a word")
            print("6. Memorize a word")
            print("7. Exit")
            choice = int(input("Enter your choice: "))
            
            if choice == 1:
                word = input("Enter the word to add: ")
                meaning = input("Enter the meaning of the word: ")
                self.add_word(word, meaning)
                print(f"{word} added to vocabulary.")
            elif choice == 2:
                word = input("Enter the word to display: ")
                self.display_word(word)
            elif choice == 3:
                word = input("Enter the word to delete: ")
                self.delete_word(word)
            elif choice == 4:
                word = input("Enter the word to update: ")
                new_meaning = input("Enter the new meaning: ")
                self.update_meaning(word, new_meaning)
            elif choice == 5:
                self.learn_word()
            elif choice == 6:
                self.memorize_word()
            elif choice == 7:
                print("Exiting...")
                break
            else:
                print("Invalid choice. Please try again.")


# 使用例子
vocab = Vocabulary()
vocab.add_word("apple", "a fruit")
vocab.add_word("cat", "a mammal")
vocab.display_word("apple")
vocab.delete_word("banana")
vocab.update_meaning("cat", "a domestic pet")
vocab.display_word("cat")
vocab.learn_word()
vocab.memorize_word()
vocab.menu()

这个Vocabulary类具有以下功能:

- add_word():向词汇表中添加新单词及其含义。

- display_word():显示指定单词的含义。

- delete_word():从词汇表中删除指定的单词。

- update_meaning():更新指定单词的含义。

- learn_word():通过用户输入来学习新单词。

- memorize_word():通过用户输入来查看指定单词的含义。

- menu():提供一个文本菜单供用户选择执行上述功能。

在这个例子中,我们首先创建了一个Vocabulary的实例,然后使用各种方法执行了不同的操作,包括添加单词、显示单词含义、删除单词、更新单词含义、学习新单词和记忆指定单词的含义。最后,我们使用menu()方法提供一个用户界面,让用户可以通过菜单选择执行操作。

请注意,这只是一个简单的实现示例,实际的词汇表类可能需要更复杂的功能和数据结构来更好地满足实际需求。