From c1f18e47c9a7f8347b6c996f4882a22f4002aa29 Mon Sep 17 00:00:00 2001 From: "patricia.saez" Date: Mon, 16 Oct 2023 17:09:39 +0200 Subject: [PATCH] Patricia S. All donegit add main.ipynb --- your-code/main.ipynb | 669 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 583 insertions(+), 86 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f3bde01..8e88779 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -24,11 +24,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 110, "id": "630d7e4d", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from functools import reduce\n", + "import os\n", + "import random\n", + "import sys" + ] }, { "cell_type": "markdown", @@ -46,6 +51,25 @@ "metadata": {}, "source": [] }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1ee26d4c", + "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": "b421e0cb", @@ -59,11 +83,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "c047115a", "metadata": {}, - "outputs": [], - "source": [] + "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, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]\n" + ] + } + ], + "source": [ + "sqrt=[i**2 for i in range(100)]\n", + "print(sqrt)\n" + ] }, { "cell_type": "markdown", @@ -77,11 +112,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "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": 15, "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 +169,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "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": 24, "id": "43b38bd3", "metadata": {}, "outputs": [], @@ -141,11 +209,29 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "d5fc7426", + "execution_count": 41, + "id": "f9aad9eb", "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']\n", + "c = 'Find all of the words in a string that are monosyllabic'\n", + "\n", + "def suma (a,b):\n", + " return a+b\n", + "\n", + "final_text=reduce(suma,[i for i in c if i not in vowels])\n", + "\n", + "print(final_text)" + ] }, { "cell_type": "markdown", @@ -160,11 +246,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 44, "id": "3a43399c", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['T', 'Q', 'B', 'F', 'J', 'O', 'T', 'L', 'D']\n" + ] + } + ], + "source": [ + "text = 'The Quick Brown Fox Jumped Over The Lazy Dog'\n", + "capital_letters = [i for i in text if i.isupper()]\n", + "print(capital_letters)" + ] }, { "cell_type": "markdown", @@ -179,11 +277,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "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": [ + "non_vowels = 'bcdfghjklmnpqrstvwxyz'\n", + "text = 'The quick brown fox jumped over the lazy dog'\n", + "\n", + "consonants = [ i for i in text.lower() if i in non_vowels]\n", + "print(consonants)" + ] }, { "cell_type": "markdown", @@ -201,11 +313,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "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": [ + "files= os.listdir('C:/PATRICIA/Ironhack/Lectures/lectures')\n", + "print(files)" + ] }, { "cell_type": "markdown", @@ -220,11 +343,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 69, "id": "120b29bd", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[49, 22, 55, 75, 55, 60, 66, 89, 26, 63], [85, 100, 50, 0, 95, 83, 38, 62, 54, 8], [93, 62, 32, 55, 78, 18, 41, 99, 25, 59], [80, 79, 81, 6, 29, 12, 64, 52, 23, 14]]\n" + ] + } + ], + "source": [ + "random_list=[[random.randint(0,100) for j in range(10)] for i in range(4)]\n", + "print(random_list)" + ] }, { "cell_type": "markdown", @@ -238,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 70, "id": "b3afebb7", "metadata": {}, "outputs": [], @@ -248,11 +382,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 73, "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 =[j for i in list_of_lists for j in i]\n", + "\n", + "print (flatten_list)" + ] }, { "cell_type": "markdown", @@ -279,11 +425,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 75, "id": "a932aff5", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]\n" + ] + } + ], + "source": [ + "floats = [[float(i) for i in j] for j in list_of_lists]\n", + "\n", + "print(floats)" + ] }, { "cell_type": "markdown", @@ -306,11 +464,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 81, "id": "3605fc78", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is a thus cannot be powered\n", + "b is a thus cannot be powered\n", + "c is a thus cannot be powered\n" + ] + } + ], + "source": [ + "for i in ['a','b','c']:\n", + " try:\n", + " print (i**2)\n", + " except TypeError:\n", + " print (f\"{i} is a {type(i)} thus cannot be powered\")\n", + " " + ] }, { "cell_type": "markdown", @@ -326,10 +501,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 82, "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[82], 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 +526,26 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 86, "id": "976fec9f", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "division by zero: All done!\n" + ] + } + ], + "source": [ + "x = 5\n", + "y = 0\n", + "try:\n", + " print(z = x/y)\n", + "except ZeroDivisionError as unalias:\n", + " print(f\"{unalias}: All done!\")" + ] }, { "cell_type": "markdown", @@ -357,10 +559,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 87, "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[87], 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 +582,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 89, "id": "cfcf15f0", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "good try byt you have a list index out of range problem...\n" + ] + } + ], + "source": [ + "abc=[10,20,20]\n", + "try:\n", + " print(abc[3])\n", + "except IndexError as alias:\n", + " print(f\"good try byt you have a {alias} problem...\")" + ] }, { "cell_type": "markdown", @@ -388,11 +616,100 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 91, "id": "3c1f141b", "metadata": {}, + "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[91], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;241m1\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;241m0\u001b[39m\n", + "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" + ] + } + ], + "source": [ + "1/0" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "b48c00e2", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "unsupported operand type(s) for /: 'int' and 'str'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[92], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;241m1\u001b[39m\u001b[38;5;241m/\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ma\u001b[39m\u001b[38;5;124m'\u001b[39m\n", + "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'int' and 'str'" + ] + } + ], + "source": [ + "1/'a'" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "360e2803", + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def divi(a,b):\n", + " try:\n", + " print(a/b)\n", + " except ZeroDivisionError as alias_1:\n", + " print(f\"upsss....carefull, it seems you have introduced a 0 as you're facing a {alias_1} error\")\n", + " except TypeError as alias_2:\n", + " print(f\"take, care, one of the digits is a string and you have a {alias_2} error\" )\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "b4ee5af6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "upsss....carefull, it seems you have introduced a 0 as you're facing a division by zero error\n" + ] + } + ], + "source": [ + "divi(5,0)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "eae12754", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "take, care, one of the digits is a string and you have a unsupported operand type(s) for /: 'str' and 'int' error\n" + ] + } + ], + "source": [ + "divi('5',2)" + ] }, { "cell_type": "markdown", @@ -406,7 +723,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 104, "id": "b73f824c", "metadata": {}, "outputs": [ @@ -415,10 +732,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[104], 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 +747,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 108, "id": "5a1cb361", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please, take a look at the providen PATH, you have an error : [Errno 2] No such file or directory: 'testfile'\n" + ] + } + ], + "source": [ + "try:\n", + " f = open('testfile','r')\n", + " f.write('Test write this')\n", + "except FileNotFoundError as alias:\n", + " print(f\"please, take a look at the providen PATH, you have an error : {alias}\")" + ] }, { "cell_type": "markdown", @@ -447,16 +779,18 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 114, "id": "5f639ab1", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "ename": "IndentationError", - "evalue": "unexpected indent (, line 2)", + "evalue": "unexpected indent (1269636024.py, 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" + "\u001b[1;36m Cell \u001b[1;32mIn[114], line 2\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" ] } ], @@ -466,6 +800,31 @@ " i = int(s.strip())\n" ] }, + { + "cell_type": "code", + "execution_count": 117, + "id": "02e354aa", + "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", + "except IndentationError:\n", + " print(\"please, revise indentation\")\n", + "except FileNotFoundError:\n", + " print(\"File not found\")" + ] + }, { "cell_type": "markdown", "id": "d695245b", @@ -481,20 +840,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 111, "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[111], line 4\u001b[0m\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[0;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[1;32m----> 4\u001b[0m linux_interaction()\n", + "Cell \u001b[1;32mIn[111], line 2\u001b[0m, in \u001b[0;36mlinux_interaction\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mlinux_interaction\u001b[39m():\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[0;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[1;31mAssertionError\u001b[0m: Function can only run on Linux systems." ] } ], @@ -507,11 +866,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "id": "1643bbed", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "def linux_interaction():\n", + " try:\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(\"it seems you are running this function in a system which is not a Linux!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "id": "8d3be18b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "it seems you are running this function in a system which is not a Linux!\n" + ] + } + ], + "source": [ + "linux_interaction()" + ] }, { "cell_type": "markdown", @@ -544,11 +929,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 134, "id": "94c2e76e", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "please, introduce an integer a\n", + "please, introduce an integer a\n", + "please, introduce an integer g\n", + "please, introduce an integer 4\n", + "16\n" + ] + } + ], + "source": [ + "def square_it():\n", + " x =input(\"please, introduce an integer \")\n", + " try:\n", + " print(int(x)**2) \n", + " except ValueError:\n", + " return square_it()\n", + "\n", + "square_it()" + ] }, { "cell_type": "markdown", @@ -563,11 +969,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 136, "id": "90d3aa37", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 9, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27, 28, 30, 32, 34, 36, 38, 40, 42, 44, 45, 46, 48, 50, 52, 54, 56, 58, 60, 62, 63, 64, 66, 68, 70, 72, 74, 76, 78, 80, 81, 82, 84, 86, 88, 90, 92, 94, 96, 98, 99, 100, 102, 104, 106, 108, 110, 112, 114, 116, 117, 118, 120, 122, 124, 126, 128, 130, 132, 134, 135, 136, 138, 140, 142, 144, 146, 148, 150, 152, 153, 154, 156, 158, 160, 162, 164, 166, 168, 170, 171, 172, 174, 176, 178, 180, 182, 184, 186, 188, 189, 190, 192, 194, 196, 198, 200, 202, 204, 206, 207, 208, 210, 212, 214, 216, 218, 220, 222, 224, 225, 226, 228, 230, 232, 234, 236, 238, 240, 242, 243, 244, 246, 248, 250, 252, 254, 256, 258, 260, 261, 262, 264, 266, 268, 270, 272, 274, 276, 278, 279, 280, 282, 284, 286, 288, 290, 292, 294, 296, 297, 298, 300, 302, 304, 306, 308, 310, 312, 314, 315, 316, 318, 320, 322, 324, 326, 328, 330, 332, 333, 334, 336, 338, 340, 342, 344, 346, 348, 350, 351, 352, 354, 356, 358, 360, 362, 364, 366, 368, 369, 370, 372, 374, 376, 378, 380, 382, 384, 386, 387, 388, 390, 392, 394, 396, 398, 400, 402, 404, 405, 406, 408, 410, 412, 414, 416, 418, 420, 422, 423, 424, 426, 428, 430, 432, 434, 436, 438, 440, 441, 442, 444, 446, 448, 450, 452, 454, 456, 458, 459, 460, 462, 464, 466, 468, 470, 472, 474, 476, 477, 478, 480, 482, 484, 486, 488, 490, 492, 494, 495, 496, 498, 500, 502, 504, 506, 508, 510, 512, 513, 514, 516, 518, 520, 522, 524, 526, 528, 530, 531, 532, 534, 536, 538, 540, 542, 544, 546, 548, 549, 550, 552, 554, 556, 558, 560, 562, 564, 566, 567, 568, 570, 572, 574, 576, 578, 580, 582, 584, 585, 586, 588, 590, 592, 594, 596, 598, 600, 602, 603, 604, 606, 608, 610, 612, 614, 616, 618, 620, 621, 622, 624, 626, 628, 630, 632, 634, 636, 638, 639, 640, 642, 644, 646, 648, 650, 652, 654, 656, 657, 658, 660, 662, 664, 666, 668, 670, 672, 674, 675, 676, 678, 680, 682, 684, 686, 688, 690, 692, 693, 694, 696, 698, 700, 702, 704, 706, 708, 710, 711, 712, 714, 716, 718, 720, 722, 724, 726, 728, 729, 730, 732, 734, 736, 738, 740, 742, 744, 746, 747, 748, 750, 752, 754, 756, 758, 760, 762, 764, 765, 766, 768, 770, 772, 774, 776, 778, 780, 782, 783, 784, 786, 788, 790, 792, 794, 796, 798, 800, 801, 802, 804, 806, 808, 810, 812, 814, 816, 818, 819, 820, 822, 824, 826, 828, 830, 832, 834, 836, 837, 838, 840, 842, 844, 846, 848, 850, 852, 854, 855, 856, 858, 860, 862, 864, 866, 868, 870, 872, 873, 874, 876, 878, 880, 882, 884, 886, 888, 890, 891, 892, 894, 896, 898, 900, 902, 904, 906, 908, 909, 910, 912, 914, 916, 918, 920, 922, 924, 926, 927, 928, 930, 932, 934, 936, 938, 940, 942, 944, 945, 946, 948, 950, 952, 954, 956, 958, 960, 962, 963, 964, 966, 968, 970, 972, 974, 976, 978, 980, 981, 982, 984, 986, 988, 990, 992, 994, 996, 998, 999, 1000]\n" + ] + } + ], + "source": [ + "results = [i for i in range (1,1001) if i % 2 == 0 or i % 9 == 0]\n", + "\n", + "print(results)" + ] }, { "cell_type": "markdown", @@ -585,7 +1003,9 @@ "cell_type": "code", "execution_count": 6, "id": "86fa19bf", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "name": "stdout", @@ -613,18 +1033,95 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 159, + "id": "6abfa850", + "metadata": {}, + "outputs": [], + "source": [ + "class Patricia_Exception (Exception):\n", + " def __init__ (self, string):\n", + " self.string = string\n", + " \n", + " def __string__ (self): #print implĂ­cito\n", + " return f\"There was an error with: {self.string}, please refer to: \"\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 157, + "id": "414cf7b8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "__main__.Patricia_Exception('Hay un error')" + ] + }, + "execution_count": 157, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "hola = Patricia_Exception(\"Hay un error\")\n", + "hola" + ] + }, + { + "cell_type": "code", + "execution_count": 158, "id": "f80bb764", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter Total Marks Scored: jm\n" + ] + }, + { + "ename": "Patricia_Exception", + "evalue": "There was an error here", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[158], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m----> 2\u001b[0m Total_Marks \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter Total Marks Scored: \u001b[39m\u001b[38;5;124m\"\u001b[39m)) \n\u001b[0;32m 3\u001b[0m Num_of_Sections \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter Num of Sections: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'jm'", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[1;31mPatricia_Exception\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[158], line 7\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m Patricia_Exception\u001b[38;5;241m.\u001b[39mlower_than_two(\u001b[38;5;28mself\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m:\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m Patricia_Exception(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThere was an error here\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[1;31mPatricia_Exception\u001b[0m: There was an error here" + ] + } + ], + "source": [ + "try:\n", + " Total_Marks = int(input(\"Enter Total Marks Scored: \")) \n", + " Num_of_Sections = int(input(\"Enter Num of Sections: \"))\n", + " if Total_Marks <2:\n", + " raise Patricia_Exception.lower_than_two(self)\n", + "except ValueError:\n", + " raise Patricia_Exception(\"There was an error here\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed3f2493", + "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack", "language": "python", - "name": "python3" + "name": "ironhack" }, "language_info": { "codemirror_mode": { @@ -636,7 +1133,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.11.5" }, "toc": { "base_numbering": 1,