{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "# ZHUDDLE | Chapter 05: Loops and iterations\n\n8 coding missions + 10 MCQs. 100 XP.\n\nAdapted from Charles R. Severance, Python for Everybody (CC BY 4.0).\n\nSource: https://www.py4e.com/lessons/loops\n\nReading: https://www.py4e.com/html3/05-iterations\n\nThe browser runs five independent cases per coding mission. This notebook supplies the first sample; input prompts here are interactive.",
      "id": "loops-000"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C01 | Count down with while\n\nUse a `while` loop to print supplied positive integer `n`, then each lower integer down through 1. Decrease n on each iteration. Finish by printing `Blastoff!`.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nn = 5\n```\n\n**Source checkpoint:** The while statement",
      "id": "loops-001"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "n = 5",
      "execution_count": null,
      "outputs": [],
      "id": "loops-002"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Count down, then announce blastoff.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-003"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C02 | Stop at done\n\nRepeatedly read text using `input()` in a while loop. When the text is `done`, use `break`. Print every other entry unchanged, then print `Done!` once after the loop.\n\nFive independent checks are worth 2 XP each. \n\nUse `input()` normally: the checker enters test values for you. Sample inputs, in order: `hello`, `done`. Input prompts are omitted from captured output.\n\n**Source checkpoint:** Infinite loops and break",
      "id": "loops-004"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Read until the sentinel is entered.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-005"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C03 | Skip comment lines\n\nRead lines until `done`. Skip lines starting with # using `continue`. Print other lines unchanged, then `Done!` after the loop. The supplied skeleton checks done for you.\n\nFive independent checks are worth 2 XP each. \n\nUse `input()` normally: the checker enters test values for you. Sample inputs, in order: `#ignore`, `hello`, `done`. Input prompts are omitted from captured output.\n\n**Source checkpoint:** Finishing iterations with continue",
      "id": "loops-006"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "while True:\n    line = input(\"> \")\n    if line == \"done\":\n        break\n    # Skip comment lines; print the others.\nprint(\"Done!\")",
      "execution_count": null,
      "outputs": [],
      "id": "loops-007"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C04 | Greet each friend\n\nUse a `for` loop over supplied `friends`. Print `Happy New Year:` followed by a space and each friend. Print `Done!` after all friends have been processed.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nfriends = ['Joseph', 'Glenn', 'Sally']\n```\n\n**Source checkpoint:** Definite loops using for",
      "id": "loops-008"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "friends = ['Joseph', 'Glenn', 'Sally']",
      "execution_count": null,
      "outputs": [],
      "id": "loops-009"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "# Greet every friend in order.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-010"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C05 | Count and accumulate\n\nUse a for loop over `numbers` to compute `count` and `total`. Initialize both to zero. Practice the loop pattern without calling len or sum. No printing is required.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nnumbers = [3, 41, 12, 9, 74, 15]\n```\n\n**Source checkpoint:** Counting and summing loops",
      "id": "loops-011"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "numbers = [3, 41, 12, 9, 74, 15]",
      "execution_count": null,
      "outputs": [],
      "id": "loops-012"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "count = 0\ntotal = 0\n# Process numbers.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-013"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C06 | Largest so far\n\nInitialize `largest` to None. Use a loop to track the greatest value in `numbers`, without calling max. For an empty list, leave largest as None.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nnumbers = [3, 41, 12, 9, 74, 15]\n```\n\n**Source checkpoint:** Maximum and minimum loops",
      "id": "loops-014"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "numbers = [3, 41, 12, 9, 74, 15]",
      "execution_count": null,
      "outputs": [],
      "id": "loops-015"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "largest = None\n# Track the largest value.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-016"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C07 | Smallest so far\n\nInitialize `smallest` to None. Use a loop to find the least value in `numbers`, without calling min. Leave None for an empty list.\n\nFive independent checks are worth 2 XP each. The checker supplies the variables below with different values on each check. Use them without replacing their starting values.\n\n```python\nnumbers = [3, 41, 12, 9, 74, 15]\n```\n\n**Source checkpoint:** Maximum and minimum loops",
      "id": "loops-017"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "numbers = [3, 41, 12, 9, 74, 15]",
      "execution_count": null,
      "outputs": [],
      "id": "loops-018"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "smallest = None\n# Track the smallest value.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-019"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## C08 | A resilient number loop\n\nRead integers until `done`. Handle invalid entries with try/except, print `Invalid input`, and continue. Track `total` and `count`. At the end, print total, count, and average separated by spaces. For no valid entries, print `No numbers` instead of dividing by zero.\n\nFive independent checks are worth 2 XP each. \n\nUse `input()` normally: the checker enters test values for you. Sample inputs, in order: `4`, `5`, `bad data`, `7`, `done`. Input prompts are omitted from captured output.\n\n**Source checkpoint:** Exercises: Exercise 1",
      "id": "loops-020"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "total = 0\ncount = 0\n# Read numbers until done, then report.",
      "execution_count": null,
      "outputs": [],
      "id": "loops-021"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M01 | Initialization\n\nBefore executing `count = count + 1`, what must be true?\n\nA. count must be zero\nB. count must already have a value\nC. count must be a string\nD. count must be a keyword",
      "id": "loops-022"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m01 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-023"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M02 | While condition\n\nWhen is the condition of a while loop evaluated?\n\nA. Only after the loop\nB. Only once\nC. Before every iteration\nD. Only after break",
      "id": "loops-024"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m02 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-025"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M03 | No iterations\n\nWhat happens if a while condition is false at its first check?\n\nA. The body runs once\nB. The program always fails\nC. The body runs forever\nD. The body is skipped",
      "id": "loops-026"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m03 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-027"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M04 | Leaving a loop\n\nWhat does break do?\n\nA. Exits the current loop\nB. Restarts the script\nC. Skips only one print\nD. Ends every function",
      "id": "loops-028"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m04 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-029"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M05 | Skipping an iteration\n\nWhat does continue do?\n\nA. Stops the interpreter\nB. Exits the loop permanently\nC. Moves to the next iteration\nD. Resets all variables",
      "id": "loops-030"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m05 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-031"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M06 | Definite iteration\n\nIn `for friend in friends:`, what is friend?\n\nA. The whole list\nB. The iteration variable\nC. A keyword\nD. A function",
      "id": "loops-032"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m06 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-033"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M07 | Counting pattern\n\nWhich update counts each processed item?\n\nA. count = item\nB. count = count + item\nC. count = 0\nD. count = count + 1",
      "id": "loops-034"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m07 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-035"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M08 | Accumulator pattern\n\nWhich update adds the current value to a running total?\n\nA. total = total + value\nB. total = value\nC. total = total + 1\nD. value = total",
      "id": "loops-036"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m08 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-037"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M09 | Before the first value\n\nWhy initialize largest to None rather than 0?\n\nA. None is a large integer\nB. None sorts above numbers\nC. It marks that no item has been seen\nD. None automatically calculates the maximum",
      "id": "loops-038"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m09 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-039"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": "## M10 | Debug by bisection\n\nWhat is the purpose of checking an intermediate value near the middle of a program?\n\nA. To double the code\nB. To narrow which portion contains the bug\nC. To remove all loops\nD. To skip input validation",
      "id": "loops-040"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": "m10 = \"\"  # A, B, C, or D",
      "execution_count": null,
      "outputs": [],
      "id": "loops-041"
    }
  ]
}
