{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "03220ff3",
   "metadata": {},
   "source": [
    "# 15-04 · Мини-проект — дневник заметок\n",
    "\n",
    "Практика к разделу [«Мини-проект: дневник заметок»](../../site/chapters/glava-15/15-04-mini-proekt-itogi.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1dc776",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Собрать чтение и запись в одном мини-проекте."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57855716",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.592354Z",
     "iopub.status.busy": "2026-08-18T15:52:50.592214Z",
     "iopub.status.idle": "2026-08-18T15:52:50.616364Z",
     "shell.execute_reply": "2026-08-18T15:52:50.615865Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Готово к новым заметкам.\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "# начинаем с чистого файла заметок для повторяемости примера\n",
    "Path(\"zametki.txt\").unlink(missing_ok=True)\n",
    "print(\"Готово к новым заметкам.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99eafd4b",
   "metadata": {},
   "source": [
    "## Про input() в этом ноутбуке\n",
    "\n",
    "Этот ноутбук выполняется автоматически, поэтому `input()` временно подменён на заранее заготовленные ответы."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c2d7b0bd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.617325Z",
     "iopub.status.busy": "2026-08-18T15:52:50.617215Z",
     "iopub.status.idle": "2026-08-18T15:52:50.619368Z",
     "shell.execute_reply": "2026-08-18T15:52:50.618981Z"
    }
   },
   "outputs": [],
   "source": [
    "_answers = iter(['Первая заметка'])\n",
    "\n",
    "def input(prompt=\"\"):\n",
    "    answer = next(_answers)\n",
    "    print(prompt + answer)\n",
    "    return answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb71ef18",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d99a845a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.620341Z",
     "iopub.status.busy": "2026-08-18T15:52:50.620162Z",
     "iopub.status.idle": "2026-08-18T15:52:50.623110Z",
     "shell.execute_reply": "2026-08-18T15:52:50.622689Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Новая заметка: Первая заметка\n",
      "Все заметки:\n",
      "- Первая заметка\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "fajl_zametok = Path(\"zametki.txt\")\n",
    "\n",
    "novaya_zametka = input(\"Новая заметка: \")\n",
    "\n",
    "with fajl_zametok.open(\"a\", encoding=\"utf-8\") as f:\n",
    "    f.write(novaya_zametka + \"\\n\")\n",
    "\n",
    "print(\"Все заметки:\")\n",
    "with fajl_zametok.open(\"r\", encoding=\"utf-8\") as f:\n",
    "    for line in f:\n",
    "        print(\"-\", line.strip())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebed6fb1",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача — ещё одна заметка (проверка дозаписи)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a92a779",
   "metadata": {},
   "source": [
    "## Про input() в этом ноутбуке\n",
    "\n",
    "Этот ноутбук выполняется автоматически, поэтому `input()` временно подменён на заранее заготовленные ответы."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "92d016af",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.624053Z",
     "iopub.status.busy": "2026-08-18T15:52:50.623946Z",
     "iopub.status.idle": "2026-08-18T15:52:50.626091Z",
     "shell.execute_reply": "2026-08-18T15:52:50.625654Z"
    }
   },
   "outputs": [],
   "source": [
    "_answers = iter(['Вторая заметка'])\n",
    "\n",
    "def input(prompt=\"\"):\n",
    "    answer = next(_answers)\n",
    "    print(prompt + answer)\n",
    "    return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f354508b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.627010Z",
     "iopub.status.busy": "2026-08-18T15:52:50.626909Z",
     "iopub.status.idle": "2026-08-18T15:52:50.629917Z",
     "shell.execute_reply": "2026-08-18T15:52:50.629441Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Новая заметка: Вторая заметка\n",
      "Все заметки:\n",
      "1. Первая заметка\n",
      "2. Вторая заметка\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "fajl_zametok = Path(\"zametki.txt\")\n",
    "novaya_zametka = input(\"Новая заметка: \")\n",
    "\n",
    "with fajl_zametok.open(\"a\", encoding=\"utf-8\") as f:\n",
    "    f.write(novaya_zametka + \"\\n\")\n",
    "\n",
    "print(\"Все заметки:\")\n",
    "with fajl_zametok.open(\"r\", encoding=\"utf-8\") as f:\n",
    "    for i, line in enumerate(f, start=1):\n",
    "        print(f\"{i}. {line.strip()}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aba74d97",
   "metadata": {},
   "source": [
    "## Проверка результата"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7afd117",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:52:50.630828Z",
     "iopub.status.busy": "2026-08-18T15:52:50.630719Z",
     "iopub.status.idle": "2026-08-18T15:52:50.633217Z",
     "shell.execute_reply": "2026-08-18T15:52:50.632812Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Обе заметки сохранились в правильном порядке.\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "lines = Path(\"zametki.txt\").read_text(encoding=\"utf-8\").splitlines()\n",
    "assert lines == [\"Первая заметка\", \"Вторая заметка\"]\n",
    "print(\"Обе заметки сохранились в правильном порядке.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "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.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
