{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.5 \u2014 Loops\n",
    "\n",
    "Every program so far has run each line exactly once, top to bottom. Loops let you repeat a block of code \u2014 either a fixed number of times, or until something changes. This is where your programs stop feeling like a list of instructions and start feeling like actual software."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Idea\n",
    "\n",
    "A **`for` loop** repeats a block a set number of times \u2014 `range(n)` gives you the numbers `0` through `n-1`:\n",
    "\n",
    "```python\n",
    "for i in range(3):\n",
    "    print(f\"Round {i + 1}\")\n",
    "```\n",
    "\n",
    "A **`while` loop** repeats as long as a condition stays `True` \u2014 use it when you don't know in advance how many times you'll need to repeat:\n",
    "\n",
    "```python\n",
    "while health > 0:\n",
    "    health = health - 10\n",
    "```\n",
    "\n",
    "`break` exits a loop immediately, even if its condition is still `True` \u2014 handy for stopping the moment something happens, rather than waiting for the loop's normal end condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A worked example \u2014 Zara explores three rooms in a row\n",
    "\n",
    "for room_number in range(3):\n",
    "    print(f\"Entering room {room_number + 1}...\")\n",
    "\n",
    "print(\"All rooms explored.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Watch out for:* a `while` loop needs something inside it that eventually makes its condition `False` \u2014 forget to update the variable it checks, and you've built an infinite loop that never stops. If a cell seems frozen, that's almost always why; use the notebook's stop/interrupt button and check your loop body."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Guided Practice 1\n",
    "\n",
    "The monster's health counts down in a `while` loop as Zara lands hits. Run the cell as-is, then change the starting health or the damage per hit and re-run."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change the values below and re-run\n",
    "\n",
    "monster_health = 40\n",
    "damage_per_hit = 15\n",
    "\n",
    "while monster_health > 0:\n",
    "    monster_health = monster_health - damage_per_hit\n",
    "    print(f\"Monster health: {monster_health}\")\n",
    "\n",
    "print(\"Monster defeated!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Guided Practice 2\n",
    "\n",
    "Old Finn knocks on doors down the corridor, one per loop, for a set number of doors. Fill in the two blanks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Fill in the two blank values, then run\n",
    "\n",
    "number_of_doors = ____   # try 4\n",
    "\n",
    "for door_number in range(____):   # try number_of_doors\n",
    "    print(f\"Old Finn knocks on door {door_number + 1}...\")\n",
    "\n",
    "print(\"All doors checked.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Independent Practice 1\n",
    "\n",
    "Simulate Zara attacking a monster 5 times in a row with a `for` loop, printing the monster's remaining health after each hit (pick any starting health and damage-per-hit values).\n",
    "\n",
    "**Hint (only if stuck):** it's almost the same shape as Guided Practice 1, but with `for attack_number in range(5):` instead of a `while` loop, and the health update moved inside that loop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here \u2014 simulate 5 attacks with a for loop, printing health after each one\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Independent Practice 2\n",
    "\n",
    "No hint this time. Write a `while` loop that keeps asking the player (with `input()`) to type a riddle's password, and only stops once they type the correct one (pick your own password and prompt text)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Your code here \u2014 a while loop that keeps asking until the correct password is typed\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Want More? (Optional)\n",
    "\n",
    "Combine a `while` loop with `break`: keep attacking the monster in a loop that would otherwise run forever (`while True:`), but `break` out of it the moment its health drops to 0 or below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Optional stretch \u2014 while True: with a break condition\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check Your Understanding\n",
    "\n",
    "In one sentence: how do you decide whether to use a `for` loop or a `while` loop?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Your answer:** *(double-click to edit this cell)*"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
