diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..997b6ac 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -24,11 +24,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 64, "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import os\n", + "import random" + ] }, { "cell_type": "markdown", @@ -46,6 +49,25 @@ "metadata": {}, "source": [] }, + { + "cell_type": "code", + "execution_count": 1, + "id": "b8016513", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]\n" + ] + } + ], + "source": [ + "square = [i**2 for i in range(1, 21)]\n", + "print(square)" + ] + }, { "cell_type": "markdown", "id": "b421e0cb", @@ -59,11 +81,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "c047115a", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1.0, 1.41, 1.73, 2.0, 2.24, 2.45, 2.65, 2.83, 3.0, 3.16, 3.32, 3.46, 3.61, 3.74, 3.87, 4.0, 4.12, 4.24, 4.36, 4.47, 4.58, 4.69, 4.8, 4.9, 5.0, 5.1, 5.2, 5.29, 5.39, 5.48, 5.57, 5.66, 5.74, 5.83, 5.92, 6.0, 6.08, 6.16, 6.24, 6.32, 6.4, 6.48, 6.56, 6.63, 6.71, 6.78, 6.86, 6.93, 7.0, 7.07, 7.14, 7.21, 7.28, 7.35, 7.42, 7.48, 7.55, 7.62, 7.68, 7.75, 7.81, 7.87, 7.94, 8.0, 8.06, 8.12, 8.19, 8.25, 8.31, 8.37, 8.43, 8.49, 8.54, 8.6, 8.66, 8.72, 8.77, 8.83, 8.89, 8.94, 9.0, 9.06, 9.11, 9.17, 9.22, 9.27, 9.33, 9.38, 9.43, 9.49, 9.54, 9.59, 9.64, 9.7, 9.75, 9.8, 9.85, 9.9, 9.95, 10.0]\n" + ] + } + ], + "source": [ + "sqrt = [round(i**(1/2), 2) for i in range(1, 101)]\n", + "print(sqrt)" + ] }, { "cell_type": "markdown", @@ -77,11 +110,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "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 +139,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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 +167,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "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 +195,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "43b38bd3", "metadata": {}, "outputs": [], @@ -141,11 +207,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "id": "d5fc7426", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fnd ll f th wrds n strng tht r mnsyllbc\n" + ] + } + ], + "source": [ + "non_vowels = \"\".join([i for i in teststring if i not in 'aeiou'])\n", + "print(non_vowels)" + ] }, { "cell_type": "markdown", @@ -160,11 +237,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "id": "3a43399c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'Q', 'B', 'F', 'J', 'O', 'T', 'L', 'D']\n" + ] + } + ], + "source": [ + "sentence = \"The Quick Brown Fox Jumped Over The Lazy Dog\"\n", + "capital_letters = [i for i in sentence if i.isupper()]\n", + "print(capital_letters)" + ] }, { "cell_type": "markdown", @@ -179,11 +268,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "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', 'O', 'v', 'r', 'T', 'h', 'L', 'z', 'y', 'D', 'g']\n" + ] + } + ], + "source": [ + "consonants = [i for i in sentence if i not in 'aeiou ']\n", + "print(consonants)" + ] }, { "cell_type": "markdown", @@ -201,11 +301,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 84, "id": "22bb13eb", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['.git', '.gitignore', 'cheatsheet', 'datasets', 'example', 'faq-and-examples', 'images', 'README.md', 'troubleshooting', 'week-1', 'week-2']\n" + ] + } + ], + "source": [ + "current_path = str(os.getcwd())\n", + "current_path = current_path.split('LABS')[0]\n", + "new_path = current_path + 'LECTURES\\lectures\\\\'\n", + "os.listdir(f\"{new_path}\")\n", + "files = [name for name in os.listdir(new_path)]\n", + "print(files)" + ] }, { "cell_type": "markdown", @@ -220,11 +335,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 39, "id": "120b29bd", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[71, 40, 49, 0, 97, 68, 66, 4, 5, 54, 92], [97, 83, 34, 34, 88, 94, 10, 20, 8, 45, 95], [35, 33, 18, 66, 38, 0, 37, 74, 70, 98, 49], [24, 100, 25, 85, 61, 70, 69, 89, 39, 78, 80]]\n" + ] + } + ], + "source": [ + "random_lists = [[random.randint(0, 101) for _ in range(11)] for _ in range(4)]\n", + "print(random_lists)" + ] }, { "cell_type": "markdown", @@ -238,7 +364,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 40, "id": "b3afebb7", "metadata": {}, "outputs": [], @@ -248,11 +374,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 42, "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 = [i for x in list_of_lists for i in x]\n", + "print(flatten_list)" + ] }, { "cell_type": "markdown", @@ -267,7 +404,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 43, "id": "7087f44e", "metadata": {}, "outputs": [], @@ -279,11 +416,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 49, "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(i) for x in list_of_lists for i in x]\n", + "print(floats)" + ] }, { "cell_type": "markdown", @@ -295,22 +443,51 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 57, "id": "b5980ab4", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for ** or pow(): 'str' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[57], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mb\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mc\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(i\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m2\u001b[39m)\n", + "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'str' and 'int'" + ] + } + ], "source": [ "for i in ['a','b','c']:\n", - " print i**2" + " print(i**2)\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "id": "3605fc78", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You can not square a . It must be float or int type.\n", + "You can not square a . It must be float or int type.\n", + "You can not square a . It must be float or int type.\n" + ] + } + ], + "source": [ + "for i in ['a','b','c']:\n", + " try:\n", + " print(i**2)\n", + " except TypeError:\n", + " print(f\"You can not square a {type(i)}. It must be float or int type.\")" + ] }, { "cell_type": "markdown", @@ -326,10 +503,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 60, "id": "441acd1c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "ZeroDivisionError", + "evalue": "division by zero", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[60], line 4\u001b[0m\n\u001b[0;32m 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m5\u001b[39m\n\u001b[0;32m 2\u001b[0m y \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m----> 4\u001b[0m z \u001b[38;5;241m=\u001b[39m x\u001b[38;5;241m/\u001b[39my\n", + "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], "source": [ "x = 5\n", "y = 0\n", @@ -339,11 +528,27 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "id": "976fec9f", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "A cannot be divided by 0.\n", + "All Done.\n" + ] + } + ], + "source": [ + "try:\n", + " z = x/y\n", + "except ZeroDivisionError:\n", + " print(f\"A {type(x)} cannot be divided by 0.\")\n", + "finally:\n", + " print(\"All Done.\")" + ] }, { "cell_type": "markdown", @@ -357,10 +562,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 63, "id": "783a8224", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndexError", + "evalue": "list index out of range", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[63], line 2\u001b[0m\n\u001b[0;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[1;32m----> 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(abc[\u001b[38;5;241m3\u001b[39m])\n", + "\u001b[1;31mIndexError\u001b[0m: list index out of range" + ] + } + ], "source": [ "abc=[10,20,20]\n", "print(abc[3])" @@ -368,11 +585,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 65, "id": "cfcf15f0", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Index out of range.\n" + ] + } + ], + "source": [ + "try:\n", + " print(abc[3])\n", + "except IndexError:\n", + " print(\"Index out of range.\")" + ] }, { "cell_type": "markdown", @@ -388,11 +618,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 95, "id": "3c1f141b", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter a number: e\n", + "Enter a second number: 5\n", + "Please enter a valid number.\n" + ] + } + ], + "source": [ + "x = input(\"Enter a number: \")\n", + "y = input(\"Enter a second number: \")\n", + "\n", + "try:\n", + " print(float(x)/float(y)) \n", + "except ZeroDivisionError:\n", + " print(\"Cannot be divided by 0.\")\n", + "except ValueError:\n", + " print(\"Please enter a valid number.\")" + ] }, { "cell_type": "markdown", @@ -406,7 +656,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 83, "id": "b73f824c", "metadata": {}, "outputs": [ @@ -415,10 +665,11 @@ "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'" + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[83], line 1\u001b[0m\n\u001b[1;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[0;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[1;32m~\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\IPython\\core\\interactiveshell.py:286\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;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[0;32m 280\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;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[0;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[0;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[0;32m 284\u001b[0m )\n\u001b[1;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[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'testfile'" ] } ], @@ -429,11 +680,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "5a1cb361", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The file you are looking for does not exists.\n" + ] + } + ], + "source": [ + "try:\n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + "except FileNotFoundError:\n", + " print(\"The file does not exists.\")" + ] }, { "cell_type": "markdown", @@ -466,6 +731,30 @@ " i = int(s.strip())\n" ] }, + { + "cell_type": "code", + "execution_count": 96, + "id": "6da806da", + "metadata": {}, + "outputs": [ + { + "ename": "IndentationError", + "evalue": "unexpected indent (1852933022.py, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m Cell \u001b[1;32mIn[96], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m line = f.readline()\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unexpected indent\n" + ] + } + ], + "source": [ + "try:\n", + " fp = open('myfile.txt')\n", + " line = f.readline()\n", + " i = int(s.strip())\n", + "except IdentationError:\n", + " print(\"Code not indented correctly\")" + ] + }, { "cell_type": "markdown", "id": "d695245b", @@ -481,24 +770,25 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 97, "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[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[97], line 5\u001b[0m\n\u001b[0;32m 3\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[0;32m 4\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[1;32m----> 5\u001b[0m linux_interaction()\n", + "Cell \u001b[1;32mIn[97], line 3\u001b[0m, in \u001b[0;36mlinux_interaction\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mlinux_interaction\u001b[39m():\n\u001b[1;32m----> 3\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[0;32m 4\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[1;31mAssertionError\u001b[0m: Function can only run on Linux systems." ] } ], "source": [ + "import sys\n", "def linux_interaction():\n", " assert ('linux' in sys.platform), \"Function can only run on Linux systems.\"\n", " print('Doing something.')\n", @@ -507,11 +797,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 99, "id": "1643bbed", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This function can only be callen from Linux systems.\n" + ] + } + ], + "source": [ + "try:\n", + " linux_interaction()\n", + "except AssertionError:\n", + " print(\"This function can only be callen from Linux systems.\")" + ] }, { "cell_type": "markdown", @@ -636,7 +939,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1,