{ "cells": [ { "cell_type": "markdown", "id": "d9c31a26", "metadata": {}, "source": [ "# Module 2 — Lab 2: Control Flow and Functions\n", "\n", "**Topics:** if/elif/else, for, while, break, continue, function\n", "\n", "\n", "> **Instructions:** Run the cells in order. Complete every `TODO`, then run the **Self-check** cell. Do not open the Solution notebook before attempting the tasks yourself.\n" ] }, { "cell_type": "markdown", "id": "e060cb80", "metadata": {}, "source": [ "## Scenario\n", "\n", "You need to build small rules that classify login alerts and automatically review a list of IP addresses. All activity is simulated with local data; this notebook does not connect to or scan any real system.\n" ] }, { "cell_type": "markdown", "id": "6d919cf5", "metadata": { "tags": [ "exercise" ] }, "source": [ "## 1. Classify failed login attempts\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ef271740", "metadata": { "tags": [ "exercise" ] }, "outputs": [], "source": [ "failed_attempts = 7\n", "\n", "# TODO 1: assign severity using the thresholds 10, 5, 1, and 0\n", "severity = None\n", "\n", "print(severity)\n" ] }, { "cell_type": "markdown", "id": "92cd839f", "metadata": { "tags": [ "exercise" ] }, "source": [ "## 2. Review IP addresses with `for`, `continue`, and `break`\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c1b87cad", "metadata": { "tags": [ "exercise" ] }, "outputs": [], "source": [ "blocked_ips = {\"10.0.0.5\", \"10.0.0.12\"}\n", "targets = [f\"10.0.0.{i}\" for i in range(1, 16)]\n", "reviewed = []\n", "\n", "for ip in targets:\n", " last_octet = int(ip.split(\".\")[-1])\n", "\n", " # TODO 2: skip IP addresses whose final octet is odd\n", " pass\n", "\n", " # TODO 3: append the IP address to reviewed\n", " pass\n", "\n", " # TODO 4: if the IP is in blocked_ips, print an alert and stop the loop\n", " pass\n", "\n", "print(\"Reviewed:\", reviewed)\n" ] }, { "cell_type": "markdown", "id": "2128332a", "metadata": { "tags": [ "exercise" ] }, "source": [ "## 3. Limit retry attempts with `while`\n" ] }, { "cell_type": "code", "execution_count": null, "id": "4fb4787e", "metadata": { "tags": [ "exercise" ] }, "outputs": [], "source": [ "attempt = 0\n", "max_attempts = 3\n", "service_responded = False\n", "\n", "# TODO 5: loop while max_attempts has not been reached and the service has not responded.\n", "# Simulate the service responding on the second attempt.\n", "while False:\n", " pass\n", "\n", "print(f\"Service responded: {service_responded}\")\n" ] }, { "cell_type": "markdown", "id": "9fc142dd", "metadata": { "tags": [ "exercise" ] }, "source": [ "## 4. Package a rule in a function\n" ] }, { "cell_type": "code", "execution_count": null, "id": "63e40cf6", "metadata": { "tags": [ "exercise" ] }, "outputs": [], "source": [ "def classify_login_risk(failed_attempts, is_admin=False, is_off_hours=False):\n", " # Return LOW, MEDIUM, HIGH, or CRITICAL for a login event.\n", " # TODO 6: calculate the score and return a label using thresholds 100, 60, and 30\n", " pass\n", "\n", "\n", "print(classify_login_risk(2))\n", "print(classify_login_risk(7, is_off_hours=True))\n", "print(classify_login_risk(8, is_admin=True))\n" ] }, { "cell_type": "markdown", "id": "a10e84bf", "metadata": {}, "source": [ "## Self-check\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b1d43daf", "metadata": {}, "outputs": [], "source": [ "assert severity == \"HIGH\"\n", "assert reviewed == [\"10.0.0.2\", \"10.0.0.4\", \"10.0.0.6\", \"10.0.0.8\", \"10.0.0.10\", \"10.0.0.12\"]\n", "assert attempt == 2 and service_responded is True\n", "assert classify_login_risk(2) == \"LOW\"\n", "assert classify_login_risk(7, is_off_hours=True) == \"HIGH\"\n", "assert classify_login_risk(8, is_admin=True) == \"CRITICAL\"\n", "print(\"✓ Lab 2 completed\")\n" ] }, { "cell_type": "markdown", "id": "3f5385ff", "metadata": {}, "source": [ "## Extension challenge\n", "\n", "Modify the function to add 20 points when `source_ip` belongs to a set of blocked IP addresses. Write at least two `assert` statements for the new rule.\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }