diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..94a1321 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -24,11 +24,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import math\n", + "import os\n", + "import random\n", + "import sys" + ] }, { "cell_type": "markdown", @@ -41,10 +46,14 @@ ] }, { - "cell_type": "markdown", - "id": "1207cb2c", + "cell_type": "code", + "execution_count": 2, + "id": "678234f2", "metadata": {}, - "source": [] + "outputs": [], + "source": [ + "square = [i**2 for i in range(1,21)]" + ] }, { "cell_type": "markdown", @@ -59,11 +68,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "c047115a", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0, 3.1622776601683795, 3.3166247903554, 3.4641016151377544, 3.605551275463989, 3.7416573867739413, 3.872983346207417, 4.0, 4.123105625617661, 4.242640687119285, 4.358898943540674, 4.47213595499958, 4.58257569495584, 4.69041575982343, 4.795831523312719, 4.898979485566356, 5.0, 5.0990195135927845, 5.196152422706632, 5.291502622129181, 5.385164807134504, 5.477225575051661, 5.5677643628300215, 5.656854249492381, 5.744562646538029, 5.830951894845301, 5.916079783099616, 6.0, 6.082762530298219, 6.164414002968976, 6.244997998398398, 6.324555320336759, 6.4031242374328485, 6.48074069840786, 6.557438524302, 6.6332495807108, 6.708203932499369, 6.782329983125268, 6.855654600401044, 6.928203230275509, 7.0, 7.0710678118654755, 7.14142842854285, 7.211102550927978, 7.280109889280518, 7.3484692283495345, 7.416198487095663, 7.483314773547883, 7.54983443527075, 7.615773105863909, 7.681145747868608, 7.745966692414834, 7.810249675906654, 7.874007874011811, 7.937253933193772, 8.0, 8.06225774829855, 8.12403840463596, 8.18535277187245, 8.246211251235321, 8.306623862918075, 8.366600265340756, 8.426149773176359, 8.48528137423857, 8.54400374531753, 8.602325267042627, 8.660254037844387, 8.717797887081348, 8.774964387392123, 8.831760866327848, 8.888194417315589, 8.94427190999916, 9.0, 9.055385138137417, 9.1104335791443, 9.16515138991168, 9.219544457292887, 9.273618495495704, 9.327379053088816, 9.38083151964686, 9.433981132056603, 9.486832980505138, 9.539392014169456, 9.591663046625438, 9.643650760992955, 9.695359714832659, 9.746794344808963, 9.797958971132712, 9.848857801796104, 9.899494936611665, 9.9498743710662, 10.0]\n" + ] + } + ], + "source": [ + "sqrt = [math.sqrt(i) for i in range(1,101)]\n", + "print(sqrt)" + ] }, { "cell_type": "markdown", @@ -77,11 +97,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "e23d87ea", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]\n" + ] + } + ], + "source": [ + "my_list = [i for i in range(-10,1)]\n", + "print(my_list)" + ] }, { "cell_type": "markdown", @@ -95,11 +126,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "d8d1c865", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]\n" + ] + } + ], + "source": [ + "odds = [i for i in range(1,101) if i%2 != 0]\n", + "print(odds)" + ] }, { "cell_type": "markdown", @@ -112,11 +154,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "6a9cfb8c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196, 203, 210, 217, 224, 231, 238, 245, 252, 259, 266, 273, 280, 287, 294, 301, 308, 315, 322, 329, 336, 343, 350, 357, 364, 371, 378, 385, 392, 399, 406, 413, 420, 427, 434, 441, 448, 455, 462, 469, 476, 483, 490, 497, 504, 511, 518, 525, 532, 539, 546, 553, 560, 567, 574, 581, 588, 595, 602, 609, 616, 623, 630, 637, 644, 651, 658, 665, 672, 679, 686, 693, 700, 707, 714, 721, 728, 735, 742, 749, 756, 763, 770, 777, 784, 791, 798, 805, 812, 819, 826, 833, 840, 847, 854, 861, 868, 875, 882, 889, 896, 903, 910, 917, 924, 931, 938, 945, 952, 959, 966, 973, 980, 987, 994]\n" + ] + } + ], + "source": [ + "divisible_by_seven = [i for i in range(1,1001) if i%7 ==0]\n", + "print(divisible_by_seven)" + ] }, { "cell_type": "markdown", @@ -129,24 +182,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "43b38bd3", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['F', 'n', 'd', ' ', 'l', 'l', ' ', 'f', ' ', 't', 'h', ' ', 'w', 'r', 'd', 's', ' ', 'n', ' ', ' ', 's', 't', 'r', 'n', 'g', ' ', 't', 'h', 't', ' ', 'r', ' ', 'm', 'n', 's', 'y', 'l', 'l', 'b', 'c']\n" + ] + } + ], "source": [ "# You can use the following test string but feel free to modify at your convenience\n", "\n", - "teststring = 'Find all of the words in a string that are monosyllabic'" + "teststring = 'Find all of the words in a string that are monosyllabic'\n", + "\n", + "non_vowels = [i for i in teststring if i not in ('a','e','i','o','u')]\n", + "\n", + "print(non_vowels)\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "d5fc7426", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "277d491e", @@ -160,11 +217,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "id": "3a43399c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'Q', 'B', 'F', 'J', 'O', 'T', 'L', 'D']\n" + ] + } + ], + "source": [ + "v_string = 'The Quick Brown Fox Jumped Over The Lazy Dog'\n", + "capital_letters= [i for i in v_string if i.isupper()]\n", + "print (capital_letters)" + ] }, { "cell_type": "markdown", @@ -179,11 +248,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "3ddb68df", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['F', 'n', 'd', ' ', 'l', 'l', ' ', 'f', ' ', 't', 'h', ' ', 'w', 'r', 'd', 's', ' ', 'n', ' ', ' ', 's', 't', 'r', 'n', 'g', ' ', 't', 'h', 't', ' ', 'r', ' ', 'm', 'n', 's', 'y', 'l', 'l', 'b', 'c']\n" + ] + } + ], + "source": [ + "v_string = 'The Quick Brown Fox Jumped Over The Lazy Dog'\n", + "\n", + "consonants = [i for i in teststring if i not in ('a','e','i','o','u')]\n", + "print(consonants)" + ] }, { "cell_type": "markdown", @@ -201,11 +283,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "22bb13eb", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['week-2', '.DS_Store', 'images', 'example', 'datasets', 'troubleshooting', 'README.md', 'week-1', '.gitignore', '.git', 'cheatsheet', 'faq-and-examples']\n" + ] + } + ], + "source": [ + "work_dir = os.chdir('/Users/pere/Documents/Ironhack/lectures/') \n", + "files = [file for file in os.listdir(work_dir)]\n", + "print(files)\n" + ] }, { "cell_type": "markdown", @@ -220,11 +314,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "120b29bd", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[5, 56, 76, 82, 72, 53, 51, 69, 51, 1], [82, 55, 49, 99, 4, 64, 2, 89, 82, 14], [71, 64, 49, 27, 71, 31, 13, 9, 85, 8], [100, 39, 24, 56, 57, 67, 38, 96, 4, 96]]\n" + ] + } + ], + "source": [ + "random_lists = [[random.randint(0,100) for i in range(10)] for i in range(4)]\n", + "\n", + "print(random_lists)" + ] }, { "cell_type": "markdown", @@ -238,22 +344,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "b3afebb7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], "source": [ - "list_of_lists = [[1,2,3],[4,5,6],[7,8,9]]" + "list_of_lists = [[1,2,3],[4,5,6],[7,8,9]]\n", + "flatten_list = [x for i in list_of_lists for x in i]\n", + "print(flatten_list)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "b6bcef37", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "53c86611", @@ -267,24 +375,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "id": "7087f44e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[40.0, 20.0, 10.0, 30.0, 20.0, 20.0, 20.0, 20.0, 20.0, 30.0, 20.0, 30.0, 20.0, 30.0, 50.0, 10.0, 30.0, 20.0, 20.0, 20.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]\n" + ] + } + ], "source": [ "list_of_lists = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], \\\n", "['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], \\\n", - "['100', '100', '100', '100']]" + "['100', '100', '100', '100']]\n", + "\n", + "floats = [float(x) for i in list_of_lists for x in i]\n", + "print(floats)" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "a932aff5", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "62272da6", @@ -295,10 +406,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "b5980ab4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "Missing parentheses in call to 'print'. Did you mean print(...)? (3439384179.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[18], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m print i**2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(...)?\n" + ] + } + ], "source": [ "for i in ['a','b','c']:\n", " print i**2" @@ -306,11 +426,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 80, "id": "3605fc78", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: You need to use number types. Strings are not allowed\n", + "ERROR: You need to use number types. Strings are not allowed\n", + "ERROR: You need to use number types. Strings are not allowed\n" + ] + } + ], + "source": [ + "for i in ['a','b','c']:\n", + " try:\n", + " print (i**2) # Syntax error fixed\n", + " except:\n", + " print(\"ERROR: You need to use number types. Strings are not allowed\")\n" + ] }, { "cell_type": "markdown", @@ -326,15 +462,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "441acd1c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: Division by zero not allowed...\n", + "All done!\n" + ] + } + ], "source": [ - "x = 5\n", - "y = 0\n", + "try:\n", + " x = 5\n", + " y = 0\n", "\n", - "z = x/y" + " z = x/y\n", + "except ZeroDivisionError:\n", + " print(\"ERROR: Division by zero not allowed...\") \n", + " \n", + "print(\"All done!\")" ] }, { @@ -357,23 +507,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "id": "783a8224", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: Element is out of expected range\n" + ] + } + ], "source": [ - "abc=[10,20,20]\n", - "print(abc[3])" + "try:\n", + " abc=[10,20,20]\n", + " print(abc[3])\n", + "except IndexError:\n", + " print(\"ERROR: Element is out of expected range\")\n" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "cfcf15f0", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "10223640", @@ -388,11 +541,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "3c1f141b", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: Division by zero not allowed...\n" + ] + } + ], + "source": [ + "\n", + "\n", + "x= input(\"Please, enter a number: \")\n", + "y= input(\"Please, enter second number: \")\n", + "\n", + "try:\n", + " print(int(x)/0)\n", + " print(x/y)\n", + " \n", + "except TypeError:\n", + " print(\"ERROR: You can't multiply strings. Please, be sure that numbers are used\")\n", + "except ZeroDivisionError:\n", + " print(\"ERROR: Division by zero not allowed...\") \n" + ] }, { "cell_type": "markdown", @@ -406,8 +581,8 @@ }, { "cell_type": "code", - "execution_count": 1, - "id": "b73f824c", + "execution_count": 24, + "id": "2749a27f", "metadata": {}, "outputs": [ { @@ -417,7 +592,8 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'testfile'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Test write this'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[1;32m/Users/pere/Documents/Ironhack/Labs/lab-errhand-listcomp/your-code/main.ipynb Cell 39\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m f \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39m(\u001b[39m'\u001b[39m\u001b[39mtestfile\u001b[39m\u001b[39m'\u001b[39m,\u001b[39m'\u001b[39m\u001b[39mr\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m 2\u001b[0m f\u001b[39m.\u001b[39mwrite(\u001b[39m'\u001b[39m\u001b[39mTest write this\u001b[39m\u001b[39m'\u001b[39m)\n", + "File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m \u001b[39mif\u001b[39;00m file \u001b[39min\u001b[39;00m {\u001b[39m0\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m}:\n\u001b[1;32m 278\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 279\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mIPython won\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt let you open fd=\u001b[39m\u001b[39m{\u001b[39;00mfile\u001b[39m}\u001b[39;00m\u001b[39m by default \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 280\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 281\u001b[0m \u001b[39m\"\u001b[39m\u001b[39myou can use builtins\u001b[39m\u001b[39m'\u001b[39m\u001b[39m open.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m )\n\u001b[0;32m--> 284\u001b[0m \u001b[39mreturn\u001b[39;00m io_open(file, \u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testfile'" ] } @@ -429,11 +605,25 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "5a1cb361", + "execution_count": 25, + "id": "b73f824c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: File is not found\n" + ] + } + ], + "source": [ + "try:\n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + "except FileNotFoundError:\n", + " print(\"ERROR: File is not found\")" + ] }, { "cell_type": "markdown", @@ -447,23 +637,74 @@ }, { "cell_type": "code", - "execution_count": 2, - "id": "5f639ab1", + "execution_count": 26, + "id": "4d5fab6e", "metadata": {}, "outputs": [ { "ename": "IndentationError", - "evalue": "unexpected indent (, line 2)", + "evalue": "unexpected indent (4187890856.py, line 2)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[26], line 2\u001b[0;36m\u001b[0m\n\u001b[0;31m line = f.readline()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" + ] + } + ], + "source": [ + "fp = open('myfile.txt')\n", + " line = f.readline()\n", + " i = int(s.strip())" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "946aae3b", + "metadata": {}, + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: 'myfile.txt'", "output_type": "error", "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m line = f.readline()\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n" + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/Users/pere/Documents/Ironhack/Labs/lab-errhand-listcomp/your-code/main.ipynb Cell 43\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m fp \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39m(\u001b[39m'\u001b[39m\u001b[39mmyfile.txt\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[1;32m 2\u001b[0m line \u001b[39m=\u001b[39m f\u001b[39m.\u001b[39mreadline()\n\u001b[1;32m 3\u001b[0m i \u001b[39m=\u001b[39m (s\u001b[39m.\u001b[39mstrip)\n", + "File \u001b[0;32m~/anaconda3/lib/python3.11/site-packages/IPython/core/interactiveshell.py:284\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 277\u001b[0m \u001b[39mif\u001b[39;00m file \u001b[39min\u001b[39;00m {\u001b[39m0\u001b[39m, \u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m}:\n\u001b[1;32m 278\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m 279\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mIPython won\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt let you open fd=\u001b[39m\u001b[39m{\u001b[39;00mfile\u001b[39m}\u001b[39;00m\u001b[39m by default \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 280\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 281\u001b[0m \u001b[39m\"\u001b[39m\u001b[39myou can use builtins\u001b[39m\u001b[39m'\u001b[39m\u001b[39m open.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m )\n\u001b[0;32m--> 284\u001b[0m \u001b[39mreturn\u001b[39;00m io_open(file, \u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n", + "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'myfile.txt'" ] } ], "source": [ "fp = open('myfile.txt')\n", + "line = f.readline()\n", + "i = (s.strip)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "5f639ab1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: File not found\n" + ] + } + ], + "source": [ + "try:\n", + " fp = open('myfile.txt')\n", " line = f.readline()\n", - " i = int(s.strip())\n" + " i = int(s.strip())\n", + "except FileNotFoundError:\n", + " print(\"ERROR: File not found\")\n", + "except TypeError:\n", + " print(\"ERROR: Wrong type. Please, enter a correct data\")\n", + "\n" ] }, { @@ -481,20 +722,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 29, "id": "3f043ef6", "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'sys' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'linux'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplatform\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Function can only run on Linux systems.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Doing something.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mlinux_interaction\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'linux'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplatform\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"Function can only run on Linux systems.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Doing something.'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mlinux_interaction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mNameError\u001b[0m: name 'sys' is not defined" + "name": "stdout", + "output_type": "stream", + "text": [ + "ERROR: This function is only allowed in Linux system \n" ] } ], @@ -502,17 +738,13 @@ "def linux_interaction():\n", " assert ('linux' in sys.platform), \"Function can only run on Linux systems.\"\n", " print('Doing something.')\n", - "linux_interaction()" + "\n", + "try:\n", + " linux_interaction()\n", + "except AssertionError:\n", + " print(\"ERROR: This function is only allowed in Linux system \")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "1643bbed", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "9310a60b", @@ -544,11 +776,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "id": "94c2e76e", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Well done!\n" + ] + } + ], + "source": [ + "def int_to_2(x):\n", + " return x**2\n", + "\n", + "while True:\n", + " x = input(\"Please, enter a number \")\n", + " try:\n", + " i = int(x)\n", + " i = int_to_2(i)\n", + " print(\"Well done!\")\n", + " break\n", + " except ValueError:\n", + " print(\"Please enter a number.\")\n", + "\n", + "\n" + ] }, { "cell_type": "markdown", @@ -563,11 +818,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "90d3aa37", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119, 120, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136, 138, 140, 141, 142, 144, 145, 146, 147, 148, 150, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 164, 165, 166, 168, 170, 171, 172, 174, 175, 176, 177, 178, 180, 182, 183, 184, 185, 186, 188, 189, 190, 192, 194, 195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 210, 212, 213, 214, 215, 216, 217, 218, 219, 220, 222, 224, 225, 226, 228, 230, 231, 232, 234, 235, 236, 237, 238, 240, 242, 243, 244, 245, 246, 248, 249, 250, 252, 254, 255, 256, 258, 259, 260, 261, 262, 264, 265, 266, 267, 268, 270, 272, 273, 274, 275, 276, 278, 279, 280, 282, 284, 285, 286, 287, 288, 290, 291, 292, 294, 295, 296, 297, 298, 300, 301, 302, 303, 304, 305, 306, 308, 309, 310, 312, 314, 315, 316, 318, 320, 321, 322, 324, 325, 326, 327, 328, 329, 330, 332, 333, 334, 335, 336, 338, 339, 340, 342, 343, 344, 345, 346, 348, 350, 351, 352, 354, 355, 356, 357, 358, 360, 362, 363, 364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 378, 380, 381, 382, 384, 385, 386, 387, 388, 390, 392, 393, 394, 395, 396, 398, 399, 400, 402, 404, 405, 406, 408, 410, 411, 412, 413, 414, 415, 416, 417, 418, 420, 422, 423, 424, 425, 426, 427, 428, 429, 430, 432, 434, 435, 436, 438, 440, 441, 442, 444, 445, 446, 447, 448, 450, 452, 453, 454, 455, 456, 458, 459, 460, 462, 464, 465, 466, 468, 469, 470, 471, 472, 474, 475, 476, 477, 478, 480, 482, 483, 484, 485, 486, 488, 489, 490, 492, 494, 495, 496, 497, 498, 500, 501, 502, 504, 505, 506, 507, 508, 510, 511, 512, 513, 514, 515, 516, 518, 519, 520, 522, 524, 525, 526, 528, 530, 531, 532, 534, 535, 536, 537, 538, 539, 540, 542, 543, 544, 545, 546, 548, 549, 550, 552, 553, 554, 555, 556, 558, 560, 561, 562, 564, 565, 566, 567, 568, 570, 572, 573, 574, 575, 576, 578, 579, 580, 581, 582, 584, 585, 586, 588, 590, 591, 592, 594, 595, 596, 597, 598, 600, 602, 603, 604, 605, 606, 608, 609, 610, 612, 614, 615, 616, 618, 620, 621, 622, 623, 624, 625, 626, 627, 628, 630, 632, 633, 634, 635, 636, 637, 638, 639, 640, 642, 644, 645, 646, 648, 650, 651, 652, 654, 655, 656, 657, 658, 660, 662, 663, 664, 665, 666, 668, 669, 670, 672, 674, 675, 676, 678, 679, 680, 681, 682, 684, 685, 686, 687, 688, 690, 692, 693, 694, 695, 696, 698, 699, 700, 702, 704, 705, 706, 707, 708, 710, 711, 712, 714, 715, 716, 717, 718, 720, 721, 722, 723, 724, 725, 726, 728, 729, 730, 732, 734, 735, 736, 738, 740, 741, 742, 744, 745, 746, 747, 748, 749, 750, 752, 753, 754, 755, 756, 758, 759, 760, 762, 763, 764, 765, 766, 768, 770, 771, 772, 774, 775, 776, 777, 778, 780, 782, 783, 784, 785, 786, 788, 789, 790, 791, 792, 794, 795, 796, 798, 800, 801, 802, 804, 805, 806, 807, 808, 810, 812, 813, 814, 815, 816, 818, 819, 820, 822, 824, 825, 826, 828, 830, 831, 832, 833, 834, 835, 836, 837, 838, 840, 842, 843, 844, 845, 846, 847, 848, 849, 850, 852, 854, 855, 856, 858, 860, 861, 862, 864, 865, 866, 867, 868, 870, 872, 873, 874, 875, 876, 878, 879, 880, 882, 884, 885, 886, 888, 889, 890, 891, 892, 894, 895, 896, 897, 898, 900, 902, 903, 904, 905, 906, 908, 909, 910, 912, 914, 915, 916, 917, 918, 920, 921, 922, 924, 925, 926, 927, 928, 930, 931, 932, 933, 934, 935, 936, 938, 939, 940, 942, 944, 945, 946, 948, 950, 951, 952, 954, 955, 956, 957, 958, 959, 960, 962, 963, 964, 965, 966, 968, 969, 970, 972, 973, 974, 975, 976, 978, 980, 981, 982, 984, 985, 986, 987, 988, 990, 992, 993, 994, 995, 996, 998, 999, 1000]\n" + ] + } + ], + "source": [ + "\n", + "l =set([n for n in range(1,1001) for n2 in range(2,10) if n % n2 == 0])\n", + "results =list(l)\n", + "print(results)" + ] }, { "cell_type": "markdown", @@ -583,41 +851,50 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "id": "86fa19bf", "metadata": {}, + "outputs": [], + "source": [ + "Total_Marks = int(input(\"Enter Total Marks Scored: \")) \n", + "Num_of_Sections = int(input(\"Enter Num of Sections: \"))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f80bb764", + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Enter Total Marks Scored: ass\n" - ] - }, - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: 'ass'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mTotal_Marks\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter Total Marks Scored: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mNum_of_Sections\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Enter Num of Sections: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'ass'" + "Number of Sections must be at least 2. Please, enter correct data\n", + "Number of Sections must be at least 2. Please, enter correct data\n", + "Number of Sections must be at least 2. Please, enter correct data\n", + "Number of Sections must be at least 2. Please, enter correct data\n", + "All Done!\n" ] } ], "source": [ - "Total_Marks = int(input(\"Enter Total Marks Scored: \")) \n", - "Num_of_Sections = int(input(\"Enter Num of Sections: \"))" + "class NumSectionsError (Exception):\n", + " pass\n", + "\n", + "\n", + "while True:\n", + " Total_Marks = int(input(\"Enter Total Marks Scored: \")) \n", + " Num_of_Sections = int(input(\"Enter Num of Sections: \")) \n", + " try:\n", + " if Num_of_Sections != 2:\n", + " raise NumSectionsError();\n", + " else:\n", + " print(\"All Done!\")\n", + " break\n", + " except NumSectionsError:\n", + " print(\"Number of Sections must be at least 2. Please, enter correct data\")" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f80bb764", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -636,7 +913,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.4" }, "toc": { "base_numbering": 1,