{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6272cc3a",
   "metadata": {},
   "source": [
    "# 13-05 · Глобальные и локальные переменные\n",
    "\n",
    "Практика к разделу [«Глобальные и локальные переменные»](../../site/chapters/glava-13/13-05-globalnye-lokalnye.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89c60aab",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Понять область видимости переменных."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f879ebfa",
   "metadata": {},
   "source": [
    "## Рабочий пример"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1fc6b79f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:38.507144Z",
     "iopub.status.busy": "2026-08-14T20:58:38.506991Z",
     "iopub.status.idle": "2026-08-14T20:58:38.529475Z",
     "shell.execute_reply": "2026-08-14T20:58:38.529064Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Версия программы: 1.0\n"
     ]
    }
   ],
   "source": [
    "ver = \"1.0\"\n",
    "\n",
    "def pokazat_versiyu():\n",
    "    print(f\"Версия программы: {ver}\")\n",
    "\n",
    "pokazat_versiyu()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b11c4af8",
   "metadata": {},
   "source": [
    "## Типичная ошибка\n",
    "\n",
    "Локальная переменная недоступна снаружи функции."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9a5822b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:38.530590Z",
     "iopub.status.busy": "2026-08-14T20:58:38.530474Z",
     "iopub.status.idle": "2026-08-14T20:58:38.557053Z",
     "shell.execute_reply": "2026-08-14T20:58:38.556648Z"
    },
    "tags": [
     "raises-exception"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Я живу только внутри функции\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'message' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mNameError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[32m      2\u001b[39m     message = \u001b[33m\"Я живу только внутри функции\"\u001b[39m\n\u001b[32m      3\u001b[39m     print(message)\n\u001b[32m      4\u001b[39m \n\u001b[32m      5\u001b[39m moya_funkciya()\n\u001b[32m----> \u001b[39m\u001b[32m6\u001b[39m print(message)\n",
      "\u001b[31mNameError\u001b[39m: name 'message' is not defined"
     ]
    }
   ],
   "source": [
    "def moya_funkciya():\n",
    "    message = \"Я живу только внутри функции\"\n",
    "    print(message)\n",
    "\n",
    "moya_funkciya()\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4610552",
   "metadata": {},
   "source": [
    "## Исправление\n",
    "\n",
    "Вернуть значение через return, если оно нужно снаружи."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "be639cb8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:38.558250Z",
     "iopub.status.busy": "2026-08-14T20:58:38.558135Z",
     "iopub.status.idle": "2026-08-14T20:58:38.560766Z",
     "shell.execute_reply": "2026-08-14T20:58:38.560161Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Теперь я возвращаюсь наружу\n"
     ]
    }
   ],
   "source": [
    "def moya_funkciya():\n",
    "    message = \"Теперь я возвращаюсь наружу\"\n",
    "    return message\n",
    "\n",
    "result = moya_funkciya()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1cff839d",
   "metadata": {},
   "source": [
    "## Эксперимент 1 — global (осторожно, редкий приём)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "507cc333",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-14T20:58:38.561920Z",
     "iopub.status.busy": "2026-08-14T20:58:38.561798Z",
     "iopub.status.idle": "2026-08-14T20:58:38.564546Z",
     "shell.execute_reply": "2026-08-14T20:58:38.564051Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "schet = 0\n",
    "\n",
    "def uvelichit_schet():\n",
    "    global schet\n",
    "    schet += 1\n",
    "\n",
    "uvelichit_schet()\n",
    "uvelichit_schet()\n",
    "print(schet)"
   ]
  }
 ],
 "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
}
