def check_dictionary(user_class: Vocabulary, word: str): """ Check the definition and example of the word by dictionary API Computational thinking: I used Decomposition to breakdown the user action into smaller tasks :precondition: user_class must be a Vocabulary class, the word must be a string has a meaning :postcondition: print the definition and an example of the word :raise KeyError, HTTPError: if the word doesn't have an definition and example, or the word doesn't exist :param user_class: must be a Vocabulary Class :param word: must be a string that has a meaning :return: the definition and an example of the word Can't do doctest and unittest because it's API related """ # Get the definition and example definition = definition_and_example(word)[0] example_sentence = definition_and_example(word)[1] # Print the definition and example of the word print(f"---- {word} ----\n" f"Definition: {definition}\n" f"Example: {example_sentence}\n") # If the word is not in the vocabulary sheet if word not in user_class.sort_words(): # Ask the user if the user wants to add the word to the vocabulary sheet user_choice = input( f"Do you want to add \'{word}\' into you vocabulary? ('Y' to add, 'N' pass):" ) # If yes, add the word to the vocabulary sheet if user_choice.strip().lower() == 'y': user_class.add_a_word(word)
def main(): """ Drives the program. """ vocabulary = Vocabulary() running = True while running: menu_list = [ "Check a word", "Add a word", "Add words", "Remove a word", "Remove words", "Print the word list", "Print the word list with definition", "Test your vocabulary", "Test your grammar" ] choice = user_input( menu_list, "Welcome to Learn English App!", "What would you like to do (Type 'quit' to exit the application): " ) if choice == "quit": running = False elif choice == "1": the_word = input("What word you want to check? : ") check_dictionary(vocabulary, the_word) elif choice == "2": new_word = input("Add a new word: ") vocabulary.add_a_word(new_word) if new_word.strip() == "": print("Error. Your word only contains whitespace.") elif choice == "3": new_words = input("Add new words (separate by commas): ") vocabulary.add_words(new_words) elif choice == "4": try: # EXCEPTION HANDLING delete_word = input("Remove a word: ") vocabulary.remove_a_word(delete_word) except KeyError: print("Error: That word is not in the vocabulary.") elif choice == "5": try: # EXCEPTION HANDLING words = input( "Enter the words you would like to remove (separate by commas): " ) vocabulary.remove_words(words) except KeyError: print( "Error: One or more of your words are not in the vocabulary." ) elif choice == "6": if len(vocabulary.sort_words()) == 0: print( "\nYour list is empty! Add some words to your list first!") else: print("\nHere is your word list") print_lists(vocabulary.sort_words()) elif choice == "7": if len(vocabulary.sort_words()) == 0: print( "\nYour list is empty! Add some words to your list first!") else: print("\nHere is your word list with definition") print_list_with_definition(vocabulary.sort_words()) elif choice == "8": try: # EXCEPTION HANDLING test_yourself(vocabulary) except IndexError: print("The question list is Empty") elif choice == "9": grammar_test()