From ed07bfb267e813984a4f933af8125ed70a4c36c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Besc=C3=B3s?= Date: Mon, 16 Oct 2023 19:26:38 +0200 Subject: [PATCH] errors no bonus vb --- your-code/main.ipynb | 464 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 385 insertions(+), 79 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..cf7c83d 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -24,11 +24,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import math \n", + "import os\n", + "import random\n", + "import sys" + ] }, { "cell_type": "markdown", @@ -40,6 +45,25 @@ "Remember to use list comprehensions and to print your results" ] }, + { + "cell_type": "code", + "execution_count": 4, + "id": "469b1dd5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]\n" + ] + } + ], + "source": [ + "square = [i**2 for i in range(20)]\n", + "print (square)" + ] + }, { "cell_type": "markdown", "id": "1207cb2c", @@ -59,11 +83,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "c047115a", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.0, 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]\n" + ] + } + ], + "source": [ + "sqrt = [math.sqrt(i) for i in range(100)]\n", + "print (sqrt)" + ] }, { "cell_type": "markdown", @@ -77,11 +112,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "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 +141,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "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, 2)]\n", + "print (odds)" + ] }, { "cell_type": "markdown", @@ -112,11 +169,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "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,7 +197,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "43b38bd3", "metadata": {}, "outputs": [], @@ -141,11 +209,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "id": "d5fc7426", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fnd ll f th wrds n strng tht r mnsyllbc\n" + ] + } + ], + "source": [ + "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\"]\n", + "\n", + "removed = \"\".join([i for i in teststring if i not in vowels])\n", + "\n", + "print (removed)\n" + ] }, { "cell_type": "markdown", @@ -160,11 +242,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "id": "3a43399c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'Q', 'B', 'F', 'J', 'O', 'T', 'L', 'D']\n" + ] + } + ], + "source": [ + "capital_letters = [i for i in \"The Quick Brown Fox Jumped Over The Lazy Dog\" if i.isupper()]\n", + "print (capital_letters)" + ] }, { "cell_type": "markdown", @@ -179,11 +272,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 58, "id": "3ddb68df", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'h', 'q', 'c', 'k', 'b', 'r', 'w', 'n', 'f', 'x', 'j', 'm', 'p', 'd', 'v', 'r', 't', 'h', 'l', 'z', 'y', 'd', 'g']\n" + ] + } + ], + "source": [ + "consonants = [i for i in \"The quick brown fox jumped over the lazy dog\" if i not in vowels and i != \" \"]\n", + "print (consonants)" + ] }, { "cell_type": "markdown", @@ -201,11 +305,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "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": [ + "files = [i for i in os.listdir(\"/Users/victorbescos/Data_analysis/lectures\")]\n", + "print (files)" + ] }, { "cell_type": "markdown", @@ -220,11 +335,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 92, "id": "120b29bd", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[55, 81, 69, 43, 64, 95, 19, 29, 24, 42, 65, 59, 97, 74, 55, 94, 52, 73, 43, 95, 55, 100, 10, 65, 27, 93, 61, 25, 77, 77, 24, 68, 83, 6, 54, 89, 13, 81, 58, 5]\n" + ] + } + ], + "source": [ + "random_lists = [random.randint(0, 101) for l in range(4) for i in range(10)]\n", + "print (random_lists)" + ] }, { "cell_type": "markdown", @@ -238,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "id": "b3afebb7", "metadata": {}, "outputs": [], @@ -248,11 +374,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 111, "id": "b6bcef37", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" + ] + } + ], + "source": [ + "flatten_list = [x for i in list_of_lists for x in i]\n", + "print (flatten_list)" + ] }, { "cell_type": "markdown", @@ -267,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 112, "id": "7087f44e", "metadata": {}, "outputs": [], @@ -279,11 +416,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 113, "id": "a932aff5", "metadata": {}, - "outputs": [], - "source": [] + "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": [ + "floats = [float(x) for i in list_of_lists for x in i]\n", + "print (floats)" + ] }, { "cell_type": "markdown", @@ -296,21 +444,41 @@ { "cell_type": "code", "execution_count": null, - "id": "b5980ab4", + "id": "d4afed3b", "metadata": {}, "outputs": [], "source": [ "for i in ['a','b','c']:\n", - " print i**2" + " print i**2\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 128, "id": "3605fc78", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "Missing parentheses in call to 'print'. Did you mean print(...)? (1451055495.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[0;36m Cell \u001b[0;32mIn[128], line 3\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", + " try: \n", + " print i**2\n", + " except SyntaxError:\n", + " print (\"Syntax Error\")\n", + " \n", + " \n", + " \n", + " " + ] }, { "cell_type": "markdown", @@ -326,10 +494,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 130, "id": "441acd1c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[130], line 4\u001b[0m\n\u001b[1;32m 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n\u001b[1;32m 2\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[0;32m----> 4\u001b[0m z \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m/\u001b[39my\n", + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], "source": [ "x = 5\n", "y = 0\n", @@ -339,11 +519,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 133, "id": "976fec9f", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Division by zero\n" + ] + } + ], + "source": [ + "try: \n", + " x = 5\n", + " y = 0\n", + "\n", + " z = x/y\n", + "except ZeroDivisionError: \n", + " print (\"Division by zero\")" + ] }, { "cell_type": "markdown", @@ -357,10 +553,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 125, "id": "783a8224", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[125], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m abc\u001b[38;5;241m=\u001b[39m[\u001b[38;5;241m10\u001b[39m,\u001b[38;5;241m20\u001b[39m,\u001b[38;5;241m20\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(abc[\u001b[38;5;241m3\u001b[39m])\n", + "\u001b[0;31mIndexError\u001b[0m: list index out of range" + ] + } + ], "source": [ "abc=[10,20,20]\n", "print(abc[3])" @@ -368,11 +576,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 132, "id": "cfcf15f0", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index error\n" + ] + } + ], + "source": [ + "try: \n", + " print(abc[3])\n", + "except IndexError:\n", + " print (\"Index error\")\n", + " \n", + " \n", + " " + ] }, { "cell_type": "markdown", @@ -388,11 +612,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "id": "3c1f141b", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "0\n", + "Second input must not be 0\n" + ] + } + ], + "source": [ + "try: \n", + " number1 = int(input())\n", + " number2 = int(input())\n", + " number3 = number1/number2\n", + " print (number3)\n", + "except ValueError: \n", + " print (\"Input must be integer\") \n", + "except ZeroDivisionError: \n", + " print (\"Second input must not be 0\")\n" + ] }, { "cell_type": "markdown", @@ -406,7 +650,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 12, "id": "b73f824c", "metadata": {}, "outputs": [ @@ -417,7 +661,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", + "Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m f \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtestfile\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 2\u001b[0m f\u001b[38;5;241m.\u001b[39mwrite(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mTest write this\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "File \u001b[0;32m~/anaconda3/envs/ironhack/lib/python3.11/site-packages/IPython/core/interactiveshell.py:286\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 279\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 280\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 281\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 283\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 284\u001b[0m )\n\u001b[0;32m--> 286\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testfile'" ] } @@ -429,11 +674,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "5a1cb361", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "File not found\n" + ] + } + ], + "source": [ + "try: \n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + " \n", + "except OSError:\n", + " print (\"File not found\")" + ] }, { "cell_type": "markdown", @@ -447,23 +707,52 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 30, "id": "5f639ab1", "metadata": {}, "outputs": [ { - "ename": "IndentationError", - "evalue": "unexpected indent (, line 2)", + "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)", + "Cell \u001b[0;32mIn[30], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m fp \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mopen\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmyfile.txt\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 2\u001b[0m line \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mreadline()\n\u001b[1;32m 3\u001b[0m i \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(s\u001b[38;5;241m.\u001b[39mstrip())\n", + "File \u001b[0;32m~/anaconda3/envs/ironhack/lib/python3.11/site-packages/IPython/core/interactiveshell.py:286\u001b[0m, in \u001b[0;36m_modified_open\u001b[0;34m(file, *args, **kwargs)\u001b[0m\n\u001b[1;32m 279\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[1;32m 280\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 281\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 282\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 283\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 284\u001b[0m )\n\u001b[0;32m--> 286\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m io_open(file, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\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 = int(s.strip())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "12ccb832", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "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 OSError: \n", + " print (\"File not found\")\n", + "except ValueError: \n", + " print (\"Value error\")\n" ] }, { @@ -481,20 +770,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "3f043ef6", "metadata": {}, "outputs": [ { - "ename": "NameError", - "evalue": "name 'sys' is not defined", + "ename": "AssertionError", + "evalue": "Function can only run on Linux systems.", "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" + "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlinux\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mplatform), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFunction can only run on Linux systems.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDoing something.\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m linux_interaction()\n", + "Cell \u001b[0;32mIn[7], line 2\u001b[0m, in \u001b[0;36mlinux_interaction\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mlinux_interaction\u001b[39m():\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlinux\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mplatform), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFunction can only run on Linux systems.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDoing something.\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "\u001b[0;31mAssertionError\u001b[0m: Function can only run on Linux systems." ] } ], @@ -507,11 +796,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "1643bbed", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Function can only run on Linux systems\n" + ] + } + ], + "source": [ + "\n", + " try: \n", + " def linux_interaction():\n", + " assert ('linux' in sys.platform), \"Function can only run on Linux systems.\"\n", + " print('Doing something.')\n", + " linux_interaction()\n", + " except AssertionError: \n", + " print (\"Function can only run on Linux systems\")\n" + ] }, { "cell_type": "markdown", @@ -622,9 +928,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack", "language": "python", - "name": "python3" + "name": "ironhack" }, "language_info": { "codemirror_mode": { @@ -636,7 +942,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1,