{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8877ea70",
   "metadata": {},
   "source": [
    "# 08-07 · Ввод от пользователя\n",
    "\n",
    "Практика к разделам [«Получение ввода от пользователей»](../../site/chapters/glava-08/08-07-vvod-polzovatelya.html) и [«Мини-проект — текст Turtle»](../../site/chapters/glava-08/08-08-mini-proekt-turtle-tekst.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab9535c9",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Освоить input() и преобразование введённого текста в числа."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9877e51",
   "metadata": {},
   "source": [
    "## Про input() в этом ноутбуке\n",
    "\n",
    "В обычном `.py`-файле `input()` ждёт, пока вы наберёте текст и нажмёте Enter. Этот ноутбук выполняется автоматически, без живого человека за клавиатурой — поэтому здесь `input()` временно подменён на заранее заготовленные ответы. Сам код с `input()` ниже выглядит и работает точно так же, как в обычном файле, который вы запускаете сами."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "66e6a5bd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.874393Z",
     "iopub.status.busy": "2026-08-14T20:53:49.874287Z",
     "iopub.status.idle": "2026-08-14T20:53:49.889347Z",
     "shell.execute_reply": "2026-08-14T20:53:49.888822Z"
    }
   },
   "outputs": [],
   "source": [
    "_answers = iter(['Cartesian', '12', '25', 'Cartesian'])\n",
    "\n",
    "def input(prompt=\"\"):\n",
    "    answer = next(_answers)\n",
    "    print(prompt + answer)\n",
    "    return answer"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "951a2832",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af82997c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.890419Z",
     "iopub.status.busy": "2026-08-14T20:53:49.890302Z",
     "iopub.status.idle": "2026-08-14T20:53:49.893099Z",
     "shell.execute_reply": "2026-08-14T20:53:49.892547Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Как вас зовут? Cartesian\n",
      "Привет, Cartesian!\n"
     ]
    }
   ],
   "source": [
    "name = input(\"Как вас зовут? \")\n",
    "print(f\"Привет, {name}!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e605ea",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — преобразование в число"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "431df4df",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.894880Z",
     "iopub.status.busy": "2026-08-14T20:53:49.894755Z",
     "iopub.status.idle": "2026-08-14T20:53:49.897043Z",
     "shell.execute_reply": "2026-08-14T20:53:49.896643Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Сколько вам лет? 12\n",
      "Через 5 лет вам будет 17.\n"
     ]
    }
   ],
   "source": [
    "age_text = input(\"Сколько вам лет? \")\n",
    "age = int(age_text)\n",
    "print(f\"Через 5 лет вам будет {age + 5}.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a6f838c",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Без `int()` строка и число не складываются."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a310aa2f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.898174Z",
     "iopub.status.busy": "2026-08-14T20:53:49.898068Z",
     "iopub.status.idle": "2026-08-14T20:53:49.924907Z",
     "shell.execute_reply": "2026-08-14T20:53:49.924388Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Сколько вам лет? 25\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[4]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m age_text = input(\u001b[33m\"Сколько вам лет? \"\u001b[39m)\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m age_text + \u001b[32m5\u001b[39m\n",
      "\u001b[31mTypeError\u001b[39m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "age_text = input(\"Сколько вам лет? \")\n",
    "age_text + 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb35747f",
   "metadata": {},
   "source": [
    "## Исправление"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "536c4b33",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.926064Z",
     "iopub.status.busy": "2026-08-14T20:53:49.925867Z",
     "iopub.status.idle": "2026-08-14T20:53:49.928160Z",
     "shell.execute_reply": "2026-08-14T20:53:49.927677Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "age = int(age_text)\n",
    "print(age + 5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b21feea",
   "metadata": {},
   "source": [
    "## Мини-проект — текст Turtle (без окна, только логика)\n",
    "\n",
    "В настоящей программе эта строка попала бы на холст через `artist.write()` (глава 7) — здесь проверим только текстовую часть."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c43380f0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:53:49.929180Z",
     "iopub.status.busy": "2026-08-14T20:53:49.929065Z",
     "iopub.status.idle": "2026-08-14T20:53:49.931246Z",
     "shell.execute_reply": "2026-08-14T20:53:49.930843Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Как вас зовут? Cartesian\n",
      "Привет, Cartesian!\n"
     ]
    }
   ],
   "source": [
    "name = input(\"Как вас зовут? \")\n",
    "message = f\"Привет, {name}!\"\n",
    "print(message)"
   ]
  }
 ],
 "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
}
