I do always search online English dictionaries for words that I know but want to get a better understanding of and/or to know their other meanings, or for words that I do not know at all. Therefore, I have written a simple program to help me search for English words faster and easier.
Here is my code:
import webbrowser class OnlineDictionary: """Represents an online dictionary. Attributes: name: str url: str """ def __init__(self, name, url, sep): """Initializes an OnlineDictionary object. name: str url: str sep: str """ self.name = name self.url = url self.sep = sep def search(self, query): """Searches the dictionary for a given query. url: str query: str """ query = self.sep.join(query.split()) webbrowser.open(self.url + query) def print_instructions(message, instructions, names, num_of_dics): """Prints the available dictionaries, their corresponding numerals and the instructions. message: str instructions: str names: list num_of_dics: int """ print(message) for numeral, name in zip(range(num_of_dics), names): print(numeral, name) print() print(instructions) def get_user_data(num_of_dics): """Asks the user for which dictionary to use and for what to search for. num_of_dics: int Returns: tuple """ while True: to_use = input('Specify which dictionary or dictionaries to use.\n') if to_use == '*': break try: to_use = to_use.replace(' ', '').split(',') numerals =  for num in to_use: num = int(num) if num not in range(num_of_dics): raise numerals.append(num) to_use = set(numerals) #set is used to get rid of any duplicates break except: continue query = input('Type something to search for.\n') return (to_use, query) def __main__(): names = ('Merriam-Webster Dictionary', 'Oxford Dictionary', 'Camberidge Dictionary', 'Collins Dictionary', 'Longman Dictionary of Contemporary English','The American Heritage Dictionary of the English Language') urls = ('https://www.merriam-webster.com/dictionary/', 'https://en.oxforddictionaries.com/definition/', 'https://dictionary.cambridge.org/dictionary/english/', 'https://www.collinsdictionary.com/dictionary/english/', 'https://www.ldoceonline.com/dictionary/', 'https://ahdictionary.com/word/search.html?q=') seps = ('%20', '_', '-', '-', '-', '%20') #seperators are used if there are spaces in the query collection = list(zip(names, urls, seps)) num_of_dics = len(names) message = "These are the available dictionaries:\n" instructions = """To use a dictionary type its corresponding numeral. To use more than one dictionary seperate the numerals with ",". To use all of them type "*". Do not type the quotation marks.\n""" print_instructions(message, instructions, names, num_of_dics) while True: to_use, query = get_user_data(num_of_dics) print() dics_to_use = collection if to_use == '*' else [collection[num] for num in to_use] dics = [OnlineDictionary(name, url, sep) for name, url, sep in dics_to_use] for dic in dics: print('Searching %s for "%s"...' % (dic.name, query)) dic.search(query) print('Finished searching. Check your Web browser.\n') if __name__ == '__main__': __main__()
I would appreciate any suggestions and comments.