{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## `Q7.` ## \n", "\n", "## ``` Create a menu drive Python program with a dictionary for words and their meanings. Write functions to add a new entry (word: meaning), search for a particular word and retrieve meaning, given meaning find words with the same meaning, remove an entry, display all words sorted alphabetically.```##" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 1\n", "\n", "Enter word:a\n", "\n", "Enter meaning:apple\n", "\n", "Do you want to continue adding words(y or n):y\n", "\n", "Enter word:b\n", "\n", "Enter meaning:bat\n", "\n", "Do you want to continue adding words(y or n):y\n", "\n", "Enter word:c\n", "\n", "Enter meaning:cat\n", "\n", "Do you want to continue adding words(y or n):y\n", "\n", "Enter word:d\n", "\n", "Enter meaning:dog\n", "\n", "Do you want to continue adding words(y or n):n\n", "\n", "Do you want to continue(y or n)?y\n", "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 2\n", "\n", "Enter word:e\n", "\n", "Enter meaning:egg\n", "\n", "Do you want to continue(y or n)?y\n", "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 2\n", "\n", "Enter word:kitten\n", "\n", "Enter meaning:cat\n", "\n", "Do you want to continue(y or n)?y\n", "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 3\n", "Enter word:b\n", "Meaning:bat\n", "\n", "Do you want to continue(y or n)?y\n", "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 4\n", "Enter meaning:cat\n", "Words with same meaning:['c', 'kitten']\n", "\n", "Do you want to continue(y or n)?y\n", "1: Create new dictionary\n", "2: Add new word\n", "3: Find meaning\n", "4: Find word with same meaning\n", "5: Display sorted list of words\n", "6: Quit\n", "Enter Choice: 5\n", "a ==> apple\n", "b ==> bat\n", "c ==> cat\n", "d ==> dog\n", "e ==> egg\n", "kitten ==> cat\n", "Sorted list of words : \n", "['a', 'b', 'c', 'd', 'e', 'kitten']\n", "\n", "Do you want to continue(y or n)?n\n" ] } ], "source": [ "word_dict = {}\n", "\n", "\n", "def create_dict():\n", " global word_dict\n", " word_dict = {}\n", " ch = \"y\"\n", " while (ch == \"y\") or (ch == \"Y\"):\n", " print(\"\\nEnter word:\", end=\"\")\n", " word = input()\n", " print(\"\\nEnter meaning:\", end=\"\")\n", " meaning = input()\n", " word_dict[word] = meaning\n", " print(\"\\nDo you want to continue adding words(y or n):\", end=\"\")\n", " ch = input()\n", "\n", "\n", "def add_word():\n", " global word_dict\n", " print(\"\\nEnter word:\", end=\"\")\n", " word = input()\n", " print(\"\\nEnter meaning:\", end=\"\")\n", " meaning = input()\n", " word_dict[word] = meaning\n", "\n", "\n", "def find_meaning(w):\n", " return word_dict[w]\n", "\n", "\n", "def find_word_same_meaning(mng):\n", " words = []\n", " for w, m in word_dict.items():\n", " if mng == m:\n", " words.append(w)\n", " return words\n", "\n", "\n", "def display_sorted():\n", " for w, m in word_dict.items():\n", " print(f\"{w} ==> {m}\")\n", " print(\"Sorted list of words : \")\n", " print(sorted(word_dict.keys()))\n", "\n", "\n", "def main():\n", " ch = \"y\"\n", " while ch == \"Y\" or ch == \"y\":\n", " print(\"1: Create new dictionary\")\n", " print(\"2: Add new word\")\n", " print(\"3: Find meaning\")\n", " print(\"4: Find word with same meaning\")\n", " print(\"5: Display sorted list of words\")\n", " print(\"6: Quit\")\n", " print(\"Enter Choice: \", end=\"\")\n", " option = int(input())\n", " if option == 1:\n", " create_dict()\n", " elif option == 2:\n", " add_word()\n", " elif option == 3:\n", " print(\"Enter word:\", end=\"\")\n", " word = input()\n", " print(\"Meaning:%s\" % (find_meaning(word)))\n", " elif option == 4:\n", " print(\"Enter meaning:\", end=\"\")\n", " meaning = input()\n", " print(\"Words with same meaning:\", end=\"\")\n", " print(find_word_same_meaning(meaning))\n", " elif option == 5:\n", " display_sorted()\n", " elif option == 6:\n", " exit()\n", " \n", "\n", " print(\"\\nDo you want to continue(y or n)?\", end=\"\")\n", " ch = input()\n", "\n", "\n", "if __name__ == \"__main__\":\n", " main()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 1 }