{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "CPSC217_6Repetition.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "Kl3axE9Ha9eW", "colab_type": "text" }, "source": [ "# Topic 6: Repetition: Loop Types" ] }, { "cell_type": "markdown", "metadata": { "id": "NRyevjlycZiN", "colab_type": "text" }, "source": [ "## Lazy" ] }, { "cell_type": "markdown", "metadata": { "id": "9D18da4gbnuk", "colab_type": "text" }, "source": [ "We are lazy and don't like to type code over and over\n", "\n", "We could copy and paste it but then if one changes we have to change the others as well\n", "\n", "Instead we have loops which let us automate the over and over process with the repeated code duplicated over and over" ] }, { "cell_type": "code", "metadata": { "id": "pWgqB-PYcKnf", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "451e4563-2588-4327-d258-6b30a5a2266b" }, "source": [ "!pip install ColabTurtle" ], "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ "Requirement already satisfied: ColabTurtle in /usr/local/lib/python3.6/dist-packages (2.0.0)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "FnbO3eAXblb2", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 541 }, "outputId": "d5fbcdb9-71b9-49f8-ad71-fb2a119546fc" }, "source": [ "#Importing \n", "from ColabTurtle import Turtle as turtle\n", "turtle.initializeTurtle()\n", "turtle.bgcolor(\"white\")\n", "turtle.right(90)\n", "\n", "#Creating Alex the turtle <- (better name could be pointer)\n", "alex = turtle\n", "\n", "#Alex is red\n", "alex.color(\"red\")\n", "\n", "#Moving and turning alex\n", "alex.forward(200)\n", "alex.right(144)\n", "alex.forward(200)\n", "alex.right(144)\n", "alex.forward(200)\n", "alex.right(144)\n", "alex.forward(200)\n", "alex.right(144)\n", "alex.forward(200)\n", "alex.right(144)" ], "execution_count": 12, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "markdown", "metadata": { "id": "EovNtEhecbtV", "colab_type": "text" }, "source": [ "## Pre-Test While Loop" ] }, { "cell_type": "markdown", "metadata": { "id": "_gYpkTp3cjZl", "colab_type": "text" }, "source": [ "while some condition is true do the body of loop over and over\n", "\n", "the condition is a boolean\n", "\n", "the body is any number of python lines" ] }, { "cell_type": "code", "metadata": { "id": "M_jKcsKLcmaW", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "5aeb68f6-ec2f-4fc6-e2ca-771f09fb1e49" }, "source": [ "i = 0\n", "while i < 3:\n", " print(i)\n", " i += 1" ], "execution_count": 9, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "W4x6_TvVd83m", "colab_type": "text" }, "source": [ "Wait what was +=" ] }, { "cell_type": "code", "metadata": { "id": "HB7wZwzid-Ol", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "6c74cd6d-8d17-4e78-a183-9ffb55742f70" }, "source": [ "i = 1\n", "i += 1\n", "print(i)\n", "\n", "j = 1\n", "j = j + 1\n", "print(j)\n", "\n", "k = 1\n", "k += 10\n", "print(k)" ], "execution_count": 16, "outputs": [ { "output_type": "stream", "text": [ "2\n", "2\n", "11\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "gS1LhgpXc5Jo", "colab_type": "text" }, "source": [ "Often if stopping point is unknown we use a sentinel to stop us when we find it" ] }, { "cell_type": "code", "metadata": { "id": "MbRMHdiec_q1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 108 }, "outputId": "b0e636df-5c12-4a0e-a455-b0c80ecca527" }, "source": [ "userInput = int(input(\"Enter a number to add to sum [0 exits]:\"))\n", "sum = 0\n", "while userInput != 0:\n", " sum += userInput\n", " userInput = int(input(\"Enter a number to add to sum [0 exits]:\"))\n", "print(\"Sum:\",sum)" ], "execution_count": 10, "outputs": [ { "output_type": "stream", "text": [ "Enter a number to add to sum:1\n", "Enter a number to add to sum:2\n", "Enter a number to add to sum:3\n", "Enter a number to add to sum:0\n", "Sum: 6\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "6zvqj2Ubc-lt", "colab_type": "text" }, "source": [ "Sometimes we re-evaluate our boolean *flag* inside the loop" ] }, { "cell_type": "code", "metadata": { "id": "nxThcfyxdb0W", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 108 }, "outputId": "83aed2af-ddac-4d57-9228-ed91793210df" }, "source": [ "looping = True\n", "sum = 0\n", "while looping:\n", " userInput = int(input(\"Enter a number to add to sum [0 exits]:\"))\n", " if userInput == 0:\n", " looping = False\n", " else:\n", " sum += userInput\n", "print(\"Sum:\", sum)\n" ], "execution_count": 11, "outputs": [ { "output_type": "stream", "text": [ "Enter a number to add to sum [0 exits]:1\n", "Enter a number to add to sum [0 exits]:2\n", "Enter a number to add to sum [0 exits]:3\n", "Enter a number to add to sum [0 exits]:0\n", "Sum: 6\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "ndIR6pAAduW1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 541 }, "outputId": "fffa0dad-6f17-46a6-9122-f4a1d0846f6b" }, "source": [ "#Importing \n", "from ColabTurtle import Turtle as turtle\n", "turtle.initializeTurtle()\n", "turtle.bgcolor(\"white\")\n", "turtle.right(90)\n", "\n", "#Creating Alex the turtle <- (better name could be pointer)\n", "alex = turtle\n", "\n", "#Alex is red\n", "alex.color(\"red\")\n", "\n", "#Moving and turning alex\n", "i = 0\n", "while i < 5:\n", " alex.forward(200)\n", " alex.right(144)\n", " i += 1\n" ], "execution_count": 14, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "code", "metadata": { "id": "7Tr-0ycRe8cl", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 163 }, "outputId": "241996fa-4910-4a40-f602-1218ad8e17c4" }, "source": [ "#Jonathan Hudson\n", "#Student Number 12347890\n", "#Date: 2020-08-26\n", "#Lecture Practice\n", "\n", "total = 0\n", "count = 0\n", "moreItems = True\n", "while moreItems:\n", " price = float(input('Enter price of item (0 when done): '))\n", " if price < 0:\n", " print(\"Enter a positive price >= 0:\")\n", " elif price != 0:\n", " count += 1\n", " total += price\n", " print('Subtotal: $%.2f' % total)\n", " else:\n", " moreItems = False\n", "\n", "\n", "print('Total items:', count)\n", "print('Total $', total)\n", "if (count != 0):\n", " average = total / count\n", " print('Average price per item: $%.2f' % average)\n", "\n" ], "execution_count": 18, "outputs": [ { "output_type": "stream", "text": [ "Enter price of item (0 when done): 10.10\n", "Subtotal: $10.10\n", "Enter price of item (0 when done): 3.15\n", "Subtotal: $13.25\n", "Enter price of item (0 when done): 0\n", "Total items: 2\n", "Total $ 13.25\n", "Average price per item: $6.62\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "1vPYD5O0fiM1", "colab_type": "text" }, "source": [ "## Pre-Test For Loop" ] }, { "cell_type": "markdown", "metadata": { "id": "A8pT-nr-fk6F", "colab_type": "text" }, "source": [ "For a list of things do the body of loop\n", "\n", "or \n", "\n", "For a known amount of steps do the body of the loop\n", "\n", "We define some variable after the for var\n", "\n", "for **var** ...\n", "\n", "which in each loop iteration will store the current item from the list" ] }, { "cell_type": "code", "metadata": { "id": "LL2zqZ4AftFt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "14b4cb69-8e7b-4c3c-e93b-a8f63bbec269" }, "source": [ "for i in range(3):\n", " print(i)" ], "execution_count": 19, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "1hYi7v6rf6S1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 145 }, "outputId": "18ea19a7-34b1-414b-8d29-13887c811df9" }, "source": [ "print(\"Enter 5 numbers to add to sum [Enter] after each\")\n", "for i in range(5):\n", " userInput = int(input(\"Enter a number to add to sum:\"))\n", " sum += userInput\n", "print(\"Sum:\",sum)" ], "execution_count": 20, "outputs": [ { "output_type": "stream", "text": [ "Enter 5 numbers to add to sum [Enter] after each\n", "Enter a number to add to sum [0 exits]:1\n", "Enter a number to add to sum [0 exits]:2\n", "Enter a number to add to sum [0 exits]:3\n", "Enter a number to add to sum [0 exits]:4\n", "Enter a number to add to sum [0 exits]:5\n", "Sum: 21\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "cqLAMpgmgJWt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "fa0d664d-4528-49fa-a0be-8c4fdc5f94c9" }, "source": [ "last = int(input(\"Please enter the number of times to loop:\"))\n", "total = 0\n", "for i in range(1, last+1, 1):\n", " total += i\n", "print(total)" ], "execution_count": 21, "outputs": [ { "output_type": "stream", "text": [ "Please enter the number of times to loop:5\n", "15\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "eZFAeN98gdBY", "colab_type": "text" }, "source": [ "What is range()? \n", "\n", "It's a generative structure that can be thought of as a list of integers (cannot do floats)" ] }, { "cell_type": "code", "metadata": { "id": "C7WVtChagl79", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "ec80dab9-83b2-4a74-f7d7-f3f70893da78" }, "source": [ "range(5)" ], "execution_count": 27, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "range(0, 5)" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "cell_type": "markdown", "metadata": { "id": "J3VT3NWyhUFW", "colab_type": "text" }, "source": [ "To truly see what a range is we have to turn it into a list(), or loop through each item" ] }, { "cell_type": "code", "metadata": { "id": "sh9pWaJJgeKd", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "f899d413-a9b3-453b-dd03-094564d4003a" }, "source": [ "list(range(5))" ], "execution_count": 26, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "metadata": { "tags": [] }, "execution_count": 26 } ] }, { "cell_type": "code", "metadata": { "id": "aNwyIuztgnpl", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "634ec00e-a4ed-4c1c-e305-5a6a52404fcd" }, "source": [ "list(range(0,5))" ], "execution_count": 28, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "metadata": { "tags": [] }, "execution_count": 28 } ] }, { "cell_type": "code", "metadata": { "id": "uph52mNlgpNt", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "69dcb0dc-ab52-4fb3-c8e4-d25a24439b34" }, "source": [ "list(range(0,5,1))" ], "execution_count": 29, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "cell_type": "code", "metadata": { "id": "zPTpeo6tgvTO", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "add19d4a-2335-4bb7-a8af-7cbc12f0e670" }, "source": [ "list(range(1,6))" ], "execution_count": 32, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "metadata": { "tags": [] }, "execution_count": 32 } ] }, { "cell_type": "code", "metadata": { "id": "gT6UDiCtgxfe", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "1f9c9103-7f4e-4309-99f5-e9c0367ccd29" }, "source": [ "list(range(0,5,2))" ], "execution_count": 34, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 2, 4]" ] }, "metadata": { "tags": [] }, "execution_count": 34 } ] }, { "cell_type": "code", "metadata": { "id": "8bLn8cn8g4m-", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "002c63dd-48f9-47b0-e570-065e63083891" }, "source": [ "for i in range(0,5,2):\n", " print(i)" ], "execution_count": 35, "outputs": [ { "output_type": "stream", "text": [ "0\n", "2\n", "4\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "GdaXa4LEhcyO", "colab_type": "text" }, "source": [ "We can also have negative steps" ] }, { "cell_type": "code", "metadata": { "id": "ctB8QurThawG", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "33397379-989e-4c2f-c299-76258154d119" }, "source": [ "list(range(5,0,-1))" ], "execution_count": 36, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[5, 4, 3, 2, 1]" ] }, "metadata": { "tags": [] }, "execution_count": 36 } ] }, { "cell_type": "code", "metadata": { "id": "fhLNaykVhcQw", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "8c907932-e301-47b3-94cf-580285de21e3" }, "source": [ "list(range(4,-1,-1))" ], "execution_count": 37, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[4, 3, 2, 1, 0]" ] }, "metadata": { "tags": [] }, "execution_count": 37 } ] }, { "cell_type": "markdown", "metadata": { "id": "4GIoLbOchpqf", "colab_type": "text" }, "source": [ "We can loop through non-range() things" ] }, { "cell_type": "code", "metadata": { "id": "s1LDIrnDhu8e", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 254 }, "outputId": "067ced0e-0c62-461d-cf0d-dece4ef9abbd" }, "source": [ "for c in \"Hello, world!\":\n", " print(c)" ], "execution_count": 38, "outputs": [ { "output_type": "stream", "text": [ "H\n", "e\n", "l\n", "l\n", "o\n", ",\n", " \n", "w\n", "o\n", "r\n", "l\n", "d\n", "!\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "Z3RteKOohyrf", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 90 }, "outputId": "4b23b280-41f6-4198-a80c-b1aa69751b73" }, "source": [ "for item in [\"Name1\", \"Name2\", \"Jonathan\", \"Hudson\"]:\n", " print(item)" ], "execution_count": 39, "outputs": [ { "output_type": "stream", "text": [ "Name1\n", "Name2\n", "Jonathan\n", "Hudson\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "zSB9BdFnh7jW", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 541 }, "outputId": "e5cb2730-e1f1-490c-c42f-419aa1e74b34" }, "source": [ "#Importing \n", "from ColabTurtle import Turtle as turtle\n", "turtle.initializeTurtle()\n", "turtle.bgcolor(\"white\")\n", "turtle.right(90)\n", "\n", "#Creating Alex the turtle <- (better name could be pointer)\n", "alex = turtle\n", "\n", "#Alex is red\n", "alex.color(\"red\")\n", "\n", "#Moving and turning alex\n", "for i in range(5):\n", " alex.forward(200)\n", " alex.right(144)\n" ], "execution_count": 40, "outputs": [ { "output_type": "display_data", "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": { "tags": [] } } ] }, { "cell_type": "markdown", "metadata": { "id": "K-smaR6ia-AN", "colab_type": "text" }, "source": [ "# Topic 6: Repetition: Loop Usage" ] }, { "cell_type": "code", "metadata": { "id": "WT0AWkDOia1Y", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "jYBHHKGgiHpe", "colab_type": "text" }, "source": [ "## The same thing" ] }, { "cell_type": "markdown", "metadata": { "id": "S7ab_WsmiTsw", "colab_type": "text" }, "source": [ "Some loops can be alternated between for and while \n", "\n", "In Python all for loops are while loops, but not all while loops are for loops.\n", "\n", "If is a bit trickier to convert a loop through String, or a list into a while loop but it can be done. We'll see that later when we reach Lists and Strings." ] }, { "cell_type": "code", "metadata": { "id": "rVQd9jQqawF4", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "b1935cc4-6a94-4f8e-9f85-79df29edd30c" }, "source": [ "sum = 0\n", "for i in range(0, 10, 1):\n", " sum += i\n", "print(sum)" ], "execution_count": 42, "outputs": [ { "output_type": "stream", "text": [ "45\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "kKC0UehyiOlJ", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "ad640474-6bd2-418b-be85-d2d40dd6da20" }, "source": [ "sum = 0\n", "i = 0\n", "while i < 10:\n", " sum += i\n", " i += 1\n", "print(sum)" ], "execution_count": 41, "outputs": [ { "output_type": "stream", "text": [ "45\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "cBSUenEQioR2", "colab_type": "text" }, "source": [ "## Break/Continue" ] }, { "cell_type": "markdown", "metadata": { "id": "XB4hfsX8iq4p", "colab_type": "text" }, "source": [ "Break lets us dump out of a loop immediately (generally done based on if condition firing)" ] }, { "cell_type": "code", "metadata": { "id": "I19bnvyCivUR", "colab_type": "code", "colab": {} }, "source": [ "for i in range(5):\n", " print(i)" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "V9ZyqmQUicKf", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 54 }, "outputId": "e8d9ffec-179c-44a2-8ecc-1cde34edc7e8" }, "source": [ "for i in range(5):\n", " if i == 2:\n", " break\n", " print(i)" ], "execution_count": 43, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "Xctz8Ji6i6ux", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "outputId": "9225c9cf-27f2-4a28-e8a0-30fa4ca01806" }, "source": [ "for i in range(5):\n", " print(i)\n", " if i == 2:\n", " break" ], "execution_count": 44, "outputs": [ { "output_type": "stream", "text": [ "0\n", "1\n", "2\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "xfN70lsyi2Qf", "colab_type": "text" }, "source": [ "Continue lets us skip the rest of a loop body (again generally done based on if condition firing)" ] }, { "cell_type": "code", "metadata": { "id": "O9eDiCH5i8cI", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 181 }, "outputId": "bfee466b-fda1-45a1-910d-b779d66d795e" }, "source": [ "for i in range(5):\n", " print(i, \"first time\")\n", " if i == 2:\n", " continue\n", " print(i, \"second time\")" ], "execution_count": 45, "outputs": [ { "output_type": "stream", "text": [ "0 first time\n", "0 second time\n", "1 first time\n", "1 second time\n", "2 first time\n", "3 first time\n", "3 second time\n", "4 first time\n", "4 second time\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "LI3ygEe0j7xa", "colab_type": "text" }, "source": [ "## Nesting" ] }, { "cell_type": "markdown", "metadata": { "id": "_8HoepYlj9YH", "colab_type": "text" }, "source": [ "Just like if elif else we can nest loops" ] }, { "cell_type": "code", "metadata": { "id": "CxcLDro_kCGo", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 290 }, "outputId": "d58a68c9-1a63-4144-ea58-813e18ef1dd5" }, "source": [ "looping = True\n", "while looping:\n", " size = int(input(\"Enter a size [<= 0 to exit]:\"))\n", " if size <= 0:\n", " looping = False\n", " else:\n", " for i in range(size):\n", " print(i)" ], "execution_count": 47, "outputs": [ { "output_type": "stream", "text": [ "Enter a size [<= 0 to exit]:1\n", "0\n", "Enter a size [<= 0 to exit]:2\n", "0\n", "1\n", "Enter a size [<= 0 to exit]:3\n", "0\n", "1\n", "2\n", "Enter a size [<= 0 to exit]:4\n", "0\n", "1\n", "2\n", "3\n", "Enter a size [<= 0 to exit]:0\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "2LHDFWyHkeeB", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "outputId": "e96b71ad-7df1-4541-eca3-a21f3f675ea6" }, "source": [ "for i in range(1,3,1): # i = [1,2]\n", " for j in range(1,4,1): #j = [1,2,3]\n", " print(\"i=%d, j=%d\"% (i,j))\n", " print(\"----------\")" ], "execution_count": 48, "outputs": [ { "output_type": "stream", "text": [ "i=1, j=1\n", "----------\n", "i=1, j=2\n", "----------\n", "i=1, j=3\n", "----------\n", "i=2, j=1\n", "----------\n", "i=2, j=2\n", "----------\n", "i=2, j=3\n", "----------\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "jBbbBNI0ksuX", "colab_type": "text" }, "source": [ "## Errors" ] }, { "cell_type": "markdown", "metadata": { "id": "a_uZMT4Bkt_n", "colab_type": "text" }, "source": [ "Infinite Loops\n" ] }, { "cell_type": "code", "metadata": { "id": "hwBamzBlkvm-", "colab_type": "code", "colab": {} }, "source": [ "i = 1\n", "while i <= 10:\n", " print(i)\n", "i += 1" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "43EUKfqrkzn3", "colab_type": "code", "colab": {} }, "source": [ "i = 1\n", "while i <= 10:\n", " print(i)\n", " i -= 1" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "Z8FCn930k59X", "colab_type": "text" }, "source": [ "Erroneous loops" ] }, { "cell_type": "code", "metadata": { "id": "7P61vfd2k662", "colab_type": "code", "colab": {} }, "source": [ "i = 10\n", "while i < 10:\n", " print(i)\n", " i += 1" ], "execution_count": 52, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "zLgumyd-lQDr", "colab_type": "text" }, "source": [ "" ] }, { "cell_type": "code", "metadata": { "id": "Y1Sjfaa7lAHw", "colab_type": "code", "colab": {} }, "source": [ "for i in range(5,0,2): #i = []\n", " print(i)" ], "execution_count": 53, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "zH_QdjrLlYwN", "colab_type": "text" }, "source": [ "## Practice" ] }, { "cell_type": "code", "metadata": { "id": "h9eKNFEHlZ7M", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 126 }, "outputId": "20f8fe87-f9d6-4b67-e974-f459296b88a0" }, "source": [ "max_multipler = int(input(\"Enter maximum multipler:\"))\n", "for i in range(1, max_multipler+1):\n", " row = \"\"\n", " for j in range(1, max_multipler+1):\n", " row += str(i*j) + \"\\t\"\n", " print(row)\n" ], "execution_count": 54, "outputs": [ { "output_type": "stream", "text": [ "Enter maximum multipler:5\n", "1\t2\t3\t4\t5\t\n", "2\t4\t6\t8\t10\t\n", "3\t6\t9\t12\t15\t\n", "4\t8\t12\t16\t20\t\n", "5\t10\t15\t20\t25\t\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "GUUr54g7lx_A", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 126 }, "outputId": "794554e3-75ae-4a06-f0ad-b00ff8715b8f" }, "source": [ "iHeight = int(input(\"Please enter the height of the triangle: \"))\n", "\n", "for i in range(iHeight):\n", "\trow = \"\"\n", "\tfor j in range(iHeight-i-1):\n", "\t\trow += \" \"\n", "\tfor j in range((i * 2) + 1):\n", "\t\trow += \"*\"\n", "\t\n", "\tprint(row)\n", "\t\n" ], "execution_count": 59, "outputs": [ { "output_type": "stream", "text": [ "Please enter the height of the triangle: 5\n", " *\n", " ***\n", " *****\n", " *******\n", "*********\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "4CdXKOkBl-_f", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 126 }, "outputId": "f8e9c120-925b-4637-e914-be8335dfea04" }, "source": [ "iHeight = int(input(\"Please enter the height of the triangle: \"))\n", "\n", "max_num_spaces = (2 * (iHeight - 1)) + 1\n", "num_spaces = max_num_spaces // 2\n", "for i in range(iHeight):\n", " num_astericks = max_num_spaces - 2 * num_spaces\n", " print(\" \" * num_spaces + \"*\" * num_astericks)\n", " num_spaces -= 1" ], "execution_count": 58, "outputs": [ { "output_type": "stream", "text": [ "Please enter the height of the triangle: 5\n", " *\n", " ***\n", " *****\n", " *******\n", "*********\n" ], "name": "stdout" } ] } ] }