|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## WIP NOTES\n", |
| 8 | + "- loop while its condition remains True\n", |
| 9 | + "- applications? -> program inputs, chemical?" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Prerequisites: \n", |
| 17 | + "- logic/boolean \n", |
| 18 | + "- comparison\n", |
| 19 | + "- `if` statements\n", |
| 20 | + "- `for` loops\n", |
| 21 | + "\n", |
| 22 | + "# Learning Objectives (WIP)\n", |
| 23 | + "- Understand how to construct and use `while` loops\n", |
| 24 | + "- Know how to control iterations in a loop\n", |
| 25 | + "\n", |
| 26 | + "# The ```while``` Loop\n", |
| 27 | + "\n", |
| 28 | + "A `while` loop is similar to a `for` loop [LINK] with the main difference being it will continuosly loop <em>while</em> a condition is `True`. As as example see the cell below which prints integers from 1 to 5." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "count=0\n", |
| 38 | + "while count<5:\n", |
| 39 | + " count+=1\n", |
| 40 | + " print(count)" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "The first line defines our starting value and the second initiates our loop.\n", |
| 48 | + "```python\n", |
| 49 | + "while(count<5):\n", |
| 50 | + "```\n", |
| 51 | + "Looking closer at this line we have:\n", |
| 52 | + "\n", |
| 53 | + "- `while` which signals we want to start a loop\n", |
| 54 | + "- `count<5` is our conditional statement\n", |
| 55 | + "- `:` signals the start of the loop\n", |
| 56 | + "\n", |
| 57 | + "If our conditional statement is `True` the code prints out the current value of `count` and increases it by one for each iteration using the `+=` operator.\n", |
| 58 | + "\n", |
| 59 | + "In general `while` loops take the structure:\n", |
| 60 | + "\n", |
| 61 | + "```python\n", |
| 62 | + "while condition:\n", |
| 63 | + " code \n", |
| 64 | + " block\n", |
| 65 | + "```" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "# Exercise 1?\n", |
| 73 | + "1. create a `while` loop to print even integers from zero to 10" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "## MORE WIP NOTES \n", |
| 88 | + "- wait for input\n", |
| 89 | + "- `break` or `continue` \n" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "## Infinite `while` loops\n", |
| 97 | + "\n", |
| 98 | + "When constructing `while` loops be wary of your condition, one common bug can be creating a loop that runs forever:\n", |
| 99 | + "\n", |
| 100 | + "```python\n", |
| 101 | + "count = 5\n", |
| 102 | + "while count != 0:\n", |
| 103 | + " print(count)\n", |
| 104 | + " count -= 2\n", |
| 105 | + "```\n", |
| 106 | + "Bug fix? or explain that condition is never met?" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": null, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "WIP EXAMPLE -- search through file until wanted line is found" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "# Password example??\n", |
| 125 | + "\n", |
| 126 | + "MAX_ATTEMPTS = 3\n", |
| 127 | + "\n", |
| 128 | + "correct_password = \"secret123\"\n", |
| 129 | + "attempts = 0\n", |
| 130 | + "\n", |
| 131 | + "while True:\n", |
| 132 | + " password = input(\"Password: \").strip()\n", |
| 133 | + " attempts += 1\n", |
| 134 | + "\n", |
| 135 | + " if password == correct_password:\n", |
| 136 | + " print(\"Login successful! Welcome!\")\n", |
| 137 | + " break\n", |
| 138 | + "\n", |
| 139 | + " if attempts >= MAX_ATTEMPTS:\n", |
| 140 | + " print(\"Too many failed attempts.\")\n", |
| 141 | + " break\n", |
| 142 | + " else:\n", |
| 143 | + " print(f\"Incorrect password. {MAX_ATTEMPTS - attempts} attempts left.\")" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "markdown", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "## TO DO\n", |
| 151 | + "- Useful chemical applications/exercises?\n", |
| 152 | + "- `break`, `continue`" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {}, |
| 158 | + "source": [] |
| 159 | + } |
| 160 | + ], |
| 161 | + "metadata": { |
| 162 | + "kernelspec": { |
| 163 | + "display_name": "base", |
| 164 | + "language": "python", |
| 165 | + "name": "python3" |
| 166 | + }, |
| 167 | + "language_info": { |
| 168 | + "codemirror_mode": { |
| 169 | + "name": "ipython", |
| 170 | + "version": 3 |
| 171 | + }, |
| 172 | + "file_extension": ".py", |
| 173 | + "mimetype": "text/x-python", |
| 174 | + "name": "python", |
| 175 | + "nbconvert_exporter": "python", |
| 176 | + "pygments_lexer": "ipython3", |
| 177 | + "version": "3.12.7" |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 2 |
| 182 | +} |
0 commit comments