From 7702ff69cc4478e28c9099d9da4056e9caf206df Mon Sep 17 00:00:00 2001 From: Marc Folch Date: Mon, 16 Oct 2023 21:52:28 +0200 Subject: [PATCH] [Marc Folch] lab-errhand-listcomp --- your-code/main.ipynb | 887 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 756 insertions(+), 131 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..2b3a04f 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -24,11 +24,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import math" + ] }, { "cell_type": "markdown", @@ -41,10 +43,46 @@ ] }, { - "cell_type": "markdown", - "id": "1207cb2c", + "cell_type": "code", + "execution_count": 28, + "id": "3a7f84c4", "metadata": {}, - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[0,\n", + " 1,\n", + " 4,\n", + " 9,\n", + " 16,\n", + " 25,\n", + " 36,\n", + " 49,\n", + " 64,\n", + " 81,\n", + " 100,\n", + " 121,\n", + " 144,\n", + " 169,\n", + " 196,\n", + " 225,\n", + " 256,\n", + " 289,\n", + " 324,\n", + " 361,\n", + " 400]" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "square = [i ** 2 for i in range(21)]\n", + "square" + ] }, { "cell_type": "markdown", @@ -59,11 +97,125 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "c047115a", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[0.0,\n", + " 1.0,\n", + " 1.4142135623730951,\n", + " 1.7320508075688772,\n", + " 2.0,\n", + " 2.23606797749979,\n", + " 2.449489742783178,\n", + " 2.6457513110645907,\n", + " 2.8284271247461903,\n", + " 3.0,\n", + " 3.1622776601683795,\n", + " 3.3166247903554,\n", + " 3.4641016151377544,\n", + " 3.605551275463989,\n", + " 3.7416573867739413,\n", + " 3.872983346207417,\n", + " 4.0,\n", + " 4.123105625617661,\n", + " 4.242640687119285,\n", + " 4.358898943540674,\n", + " 4.47213595499958,\n", + " 4.58257569495584,\n", + " 4.69041575982343,\n", + " 4.795831523312719,\n", + " 4.898979485566356,\n", + " 5.0,\n", + " 5.0990195135927845,\n", + " 5.196152422706632,\n", + " 5.291502622129181,\n", + " 5.385164807134504,\n", + " 5.477225575051661,\n", + " 5.5677643628300215,\n", + " 5.656854249492381,\n", + " 5.744562646538029,\n", + " 5.830951894845301,\n", + " 5.916079783099616,\n", + " 6.0,\n", + " 6.082762530298219,\n", + " 6.164414002968976,\n", + " 6.244997998398398,\n", + " 6.324555320336759,\n", + " 6.4031242374328485,\n", + " 6.48074069840786,\n", + " 6.557438524302,\n", + " 6.6332495807108,\n", + " 6.708203932499369,\n", + " 6.782329983125268,\n", + " 6.855654600401044,\n", + " 6.928203230275509,\n", + " 7.0,\n", + " 7.0710678118654755,\n", + " 7.14142842854285,\n", + " 7.211102550927978,\n", + " 7.280109889280518,\n", + " 7.3484692283495345,\n", + " 7.416198487095663,\n", + " 7.483314773547883,\n", + " 7.54983443527075,\n", + " 7.615773105863909,\n", + " 7.681145747868608,\n", + " 7.745966692414834,\n", + " 7.810249675906654,\n", + " 7.874007874011811,\n", + " 7.937253933193772,\n", + " 8.0,\n", + " 8.06225774829855,\n", + " 8.12403840463596,\n", + " 8.18535277187245,\n", + " 8.246211251235321,\n", + " 8.306623862918075,\n", + " 8.366600265340756,\n", + " 8.426149773176359,\n", + " 8.48528137423857,\n", + " 8.54400374531753,\n", + " 8.602325267042627,\n", + " 8.660254037844387,\n", + " 8.717797887081348,\n", + " 8.774964387392123,\n", + " 8.831760866327848,\n", + " 8.888194417315589,\n", + " 8.94427190999916,\n", + " 9.0,\n", + " 9.055385138137417,\n", + " 9.1104335791443,\n", + " 9.16515138991168,\n", + " 9.219544457292887,\n", + " 9.273618495495704,\n", + " 9.327379053088816,\n", + " 9.38083151964686,\n", + " 9.433981132056603,\n", + " 9.486832980505138,\n", + " 9.539392014169456,\n", + " 9.591663046625438,\n", + " 9.643650760992955,\n", + " 9.695359714832659,\n", + " 9.746794344808963,\n", + " 9.797958971132712,\n", + " 9.848857801796104,\n", + " 9.899494936611665,\n", + " 9.9498743710662,\n", + " 10.0]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sqrt = [math.sqrt(i) for i in range(101)]\n", + "sqrt" + ] }, { "cell_type": "markdown", @@ -77,11 +229,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "id": "e23d87ea", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0]" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "my_list = [-i for i in range(10, -1, -1)]\n", + "my_list" + ] }, { "cell_type": "markdown", @@ -95,11 +261,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "d8d1c865", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[1,\n", + " 3,\n", + " 5,\n", + " 7,\n", + " 9,\n", + " 11,\n", + " 13,\n", + " 15,\n", + " 17,\n", + " 19,\n", + " 21,\n", + " 23,\n", + " 25,\n", + " 27,\n", + " 29,\n", + " 31,\n", + " 33,\n", + " 35,\n", + " 37,\n", + " 39,\n", + " 41,\n", + " 43,\n", + " 45,\n", + " 47,\n", + " 49,\n", + " 51,\n", + " 53,\n", + " 55,\n", + " 57,\n", + " 59,\n", + " 61,\n", + " 63,\n", + " 65,\n", + " 67,\n", + " 69,\n", + " 71,\n", + " 73,\n", + " 75,\n", + " 77,\n", + " 79,\n", + " 81,\n", + " 83,\n", + " 85,\n", + " 87,\n", + " 89,\n", + " 91,\n", + " 93,\n", + " 95,\n", + " 97,\n", + " 99]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "odds = [i for i in range(100) if i %2 != 0]\n", + "odds" + ] }, { "cell_type": "markdown", @@ -112,11 +341,166 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "id": "6a9cfb8c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[7,\n", + " 14,\n", + " 21,\n", + " 28,\n", + " 35,\n", + " 42,\n", + " 49,\n", + " 56,\n", + " 63,\n", + " 70,\n", + " 77,\n", + " 84,\n", + " 91,\n", + " 98,\n", + " 105,\n", + " 112,\n", + " 119,\n", + " 126,\n", + " 133,\n", + " 140,\n", + " 147,\n", + " 154,\n", + " 161,\n", + " 168,\n", + " 175,\n", + " 182,\n", + " 189,\n", + " 196,\n", + " 203,\n", + " 210,\n", + " 217,\n", + " 224,\n", + " 231,\n", + " 238,\n", + " 245,\n", + " 252,\n", + " 259,\n", + " 266,\n", + " 273,\n", + " 280,\n", + " 287,\n", + " 294,\n", + " 301,\n", + " 308,\n", + " 315,\n", + " 322,\n", + " 329,\n", + " 336,\n", + " 343,\n", + " 350,\n", + " 357,\n", + " 364,\n", + " 371,\n", + " 378,\n", + " 385,\n", + " 392,\n", + " 399,\n", + " 406,\n", + " 413,\n", + " 420,\n", + " 427,\n", + " 434,\n", + " 441,\n", + " 448,\n", + " 455,\n", + " 462,\n", + " 469,\n", + " 476,\n", + " 483,\n", + " 490,\n", + " 497,\n", + " 504,\n", + " 511,\n", + " 518,\n", + " 525,\n", + " 532,\n", + " 539,\n", + " 546,\n", + " 553,\n", + " 560,\n", + " 567,\n", + " 574,\n", + " 581,\n", + " 588,\n", + " 595,\n", + " 602,\n", + " 609,\n", + " 616,\n", + " 623,\n", + " 630,\n", + " 637,\n", + " 644,\n", + " 651,\n", + " 658,\n", + " 665,\n", + " 672,\n", + " 679,\n", + " 686,\n", + " 693,\n", + " 700,\n", + " 707,\n", + " 714,\n", + " 721,\n", + " 728,\n", + " 735,\n", + " 742,\n", + " 749,\n", + " 756,\n", + " 763,\n", + " 770,\n", + " 777,\n", + " 784,\n", + " 791,\n", + " 798,\n", + " 805,\n", + " 812,\n", + " 819,\n", + " 826,\n", + " 833,\n", + " 840,\n", + " 847,\n", + " 854,\n", + " 861,\n", + " 868,\n", + " 875,\n", + " 882,\n", + " 889,\n", + " 896,\n", + " 903,\n", + " 910,\n", + " 917,\n", + " 924,\n", + " 931,\n", + " 938,\n", + " 945,\n", + " 952,\n", + " 959,\n", + " 966,\n", + " 973,\n", + " 980,\n", + " 987,\n", + " 994]" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divisible_by_seven = [i for i in range(1000) if i %7 == 0 and i > 0]\n", + "divisible_by_seven" + ] }, { "cell_type": "markdown", @@ -129,23 +513,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 37, "id": "43b38bd3", "metadata": {}, "outputs": [], "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'" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "id": "d5fc7426", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['F',\n", + " 'n',\n", + " 'd',\n", + " ' ',\n", + " 'l',\n", + " 'l',\n", + " ' ',\n", + " 'f',\n", + " ' ',\n", + " 't',\n", + " 'h',\n", + " ' ',\n", + " 'w',\n", + " 'r',\n", + " 'd',\n", + " 's',\n", + " ' ',\n", + " 'n',\n", + " ' ',\n", + " ' ',\n", + " 's',\n", + " 't',\n", + " 'r',\n", + " 'n',\n", + " 'g',\n", + " ' ',\n", + " 't',\n", + " 'h',\n", + " 't',\n", + " ' ',\n", + " 'r',\n", + " ' ',\n", + " 'm',\n", + " 'n',\n", + " 's',\n", + " 'y',\n", + " 'l',\n", + " 'l',\n", + " 'b',\n", + " 'c']" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vocals = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n", + "[i for i in teststring if i not in vocals]" + ] }, { "cell_type": "markdown", @@ -160,11 +595,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 77, "id": "3a43399c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "'TQBFJOTLD'" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = \"The Quick Brown Fox Jumped Over The Lazy Dog\"\n", + "b = \"\"\n", + "a = a.split(\" \")\n", + "for i in a:\n", + " b = b + i[0]\n", + "\n", + "b" + ] }, { "cell_type": "markdown", @@ -179,11 +633,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 97, "id": "3ddb68df", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "{'T',\n", + " 'b',\n", + " 'c',\n", + " 'd',\n", + " 'f',\n", + " 'g',\n", + " 'h',\n", + " 'j',\n", + " 'k',\n", + " 'l',\n", + " 'm',\n", + " 'n',\n", + " 'p',\n", + " 'q',\n", + " 'r',\n", + " 't',\n", + " 'v',\n", + " 'w',\n", + " 'x',\n", + " 'y',\n", + " 'z'}" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = \"The quick brown fox jumped over the lazy dog\"\n", + "b = \"\"\n", + "a = a.replace(\" \", \"\")\n", + "for i in a:\n", + " if i not in vocals:\n", + " b += i\n", + " \n", + "set(b)" + ] }, { "cell_type": "markdown", @@ -201,11 +695,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "id": "22bb13eb", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "['.git',\n", + " '.gitignore',\n", + " 'cheatsheet',\n", + " 'datasets',\n", + " 'example',\n", + " 'faq-and-examples',\n", + " 'images',\n", + " 'README.md',\n", + " 'troubleshooting',\n", + " 'week-1',\n", + " 'week-2']" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import os\n", + "\n", + "extract = os.listdir(\"C:/Users/marcf/Desktop/lectures\")\n", + "\n", + "extract" + ] }, { "cell_type": "markdown", @@ -220,11 +741,30 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 134, "id": "120b29bd", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[[78, 44, 97, 87, 79, 52, 74, 32, 29, 9],\n", + " [25, 0, 39, 34, 82, 94, 7, 36, 67, 41],\n", + " [81, 51, 13, 65, 77, 41, 38, 15, 69, 31],\n", + " [70, 32, 24, 76, 58, 89, 53, 63, 10, 66]]" + ] + }, + "execution_count": 134, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import random\n", + "r = range(100)\n", + "\n", + "[random.sample(r,10) for i in range(4)]" + ] }, { "cell_type": "markdown", @@ -238,7 +778,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 147, "id": "b3afebb7", "metadata": {}, "outputs": [], @@ -248,11 +788,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 148, "id": "b6bcef37", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + }, + "execution_count": 148, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "flatten_list = [j for i in list_of_lists for j in i]\n", + "flatten_list" + ] }, { "cell_type": "markdown", @@ -267,7 +821,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 149, "id": "7087f44e", "metadata": {}, "outputs": [], @@ -279,11 +833,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 150, "id": "a932aff5", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "[40,\n", + " 20,\n", + " 10,\n", + " 30,\n", + " 20,\n", + " 20,\n", + " 20,\n", + " 20,\n", + " 20,\n", + " 30,\n", + " 20,\n", + " 30,\n", + " 20,\n", + " 30,\n", + " 50,\n", + " 10,\n", + " 30,\n", + " 20,\n", + " 20,\n", + " 20,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100,\n", + " 100]" + ] + }, + "execution_count": 150, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "floats = [int(j) for i in list_of_lists for j in i]\n", + "floats" + ] }, { "cell_type": "markdown", @@ -295,23 +893,28 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "b5980ab4", + "execution_count": 191, + "id": "3605fc78", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TypeError\n", + "TypeError\n", + "TypeError\n" + ] + } + ], "source": [ "for i in ['a','b','c']:\n", - " print i**2" + " try:\n", + " print(i ** 2)\n", + " except TypeError:\n", + " print(\"TypeError\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "3605fc78", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "bda76246", @@ -326,25 +929,31 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "441acd1c", + "execution_count": 195, + "id": "976fec9f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Don't divide by 0\n", + "All Done\n" + ] + } + ], "source": [ "x = 5\n", "y = 0\n", "\n", - "z = x/y" + "try:\n", + " print(x / y)\n", + "except ZeroDivisionError:\n", + " print(\"Don't divide by 0\")\n", + "finally:\n", + " print(\"All Done\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "976fec9f", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "85ea78c5", @@ -357,23 +966,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 197, "id": "783a8224", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You are trying to find an item that is out of range\n" + ] + } + ], "source": [ "abc=[10,20,20]\n", - "print(abc[3])" + "\n", + "try:\n", + " print(abc[3])\n", + "except IndexError:\n", + " print(\"You are trying to find an item that is out of range\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "cfcf15f0", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "10223640", @@ -388,11 +1001,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 202, "id": "3c1f141b", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "0\n", + "Don't divide by 0\n" + ] + } + ], + "source": [ + "a = input()\n", + "b = input()\n", + "\n", + "try:\n", + " print(int(a)/int(b))\n", + "\n", + "except ZeroDivisionError:\n", + " print(\"Don't divide by 0\")\n", + "\n", + "except ValueError:\n", + " print(\"Only numbers\")" + ] }, { "cell_type": "markdown", @@ -406,35 +1041,26 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 218, "id": "b73f824c", "metadata": {}, "outputs": [ { - "ename": "FileNotFoundError", - "evalue": "[Errno 2] No such file or directory: 'testfile'", - "output_type": "error", - "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[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testfile'" + "name": "stdout", + "output_type": "stream", + "text": [ + "file not found\n" ] } ], "source": [ - "f = open('testfile','r')\n", - "f.write('Test write this')" + "try:\n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + "except FileNotFoundError:\n", + " print(\"file not found\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "5a1cb361", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "a028ccb8", @@ -447,23 +1073,32 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 219, "id": "5f639ab1", "metadata": {}, "outputs": [ { - "ename": "IndentationError", - "evalue": "unexpected indent (, line 2)", - "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" + "name": "stdout", + "output_type": "stream", + "text": [ + "file or directory not found\n" ] } ], "source": [ - "fp = open('myfile.txt')\n", + "try:\n", + " fp = open('myfile.txt')\n", " line = f.readline()\n", - " i = int(s.strip())\n" + " i = int(s.strip())\n", + "\n", + "except IndentationError:\n", + " print(\"unexpected indent\")\n", + " \n", + "except FileNotFoundError:\n", + " print(\"file or directory not found\")\n", + " \n", + "except ValueError:\n", + " print(\"data could not be converted to int\")" ] }, { @@ -481,38 +1116,28 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 222, "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": [ + "sys not available\n" ] } ], "source": [ - "def linux_interaction():\n", - " assert ('linux' in sys.platform), \"Function can only run on Linux systems.\"\n", - " print('Doing something.')\n", - "linux_interaction()" + "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 NameError:\n", + " print(\"sys not available\")" ] }, - { - "cell_type": "code", - "execution_count": null, - "id": "1643bbed", - "metadata": {}, - "outputs": [], - "source": [] - }, { "cell_type": "markdown", "id": "9310a60b", @@ -636,7 +1261,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.9.13" }, "toc": { "base_numbering": 1,