{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "550e7540",
   "metadata": {},
   "source": [
    "# 13-05 · Zmienne globalne i lokalne\n",
    "\n",
    "Praktyka do sekcji [„Globalne i lokalne zmienne”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58338d86",
   "metadata": {},
   "source": [
    "## Cel\n",
    "\n",
    "Zrozum zasięg zmienne."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62d5acfe",
   "metadata": {},
   "source": [
    "## Sprawa robocza"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "013b708a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T18:53:43.693788Z",
     "iopub.status.busy": "2026-08-17T18:53:43.693685Z",
     "iopub.status.idle": "2026-08-17T18:53:43.712770Z",
     "shell.execute_reply": "2026-08-17T18:53:43.712365Z"
    }
   },
   "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": "caf106b5",
   "metadata": {},
   "source": [
    "## Typowy błąd\n",
    "\n",
    "Lokalny zmienna nie jest dostępny spoza tej uroczystości."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c18f94c8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T18:53:43.713943Z",
     "iopub.status.busy": "2026-08-17T18:53:43.713818Z",
     "iopub.status.idle": "2026-08-17T18:53:43.740689Z",
     "shell.execute_reply": "2026-08-17T18:53:43.740180Z"
    },
    "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": "97028e48",
   "metadata": {},
   "source": [
    "## Poprawka\n",
    "\n",
    "Zwróć wartość po return, jeśli jest potrzebna na zewnątrz."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "73139791",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T18:53:43.741681Z",
     "iopub.status.busy": "2026-08-17T18:53:43.741571Z",
     "iopub.status.idle": "2026-08-17T18:53:43.744039Z",
     "shell.execute_reply": "2026-08-17T18:53:43.743599Z"
    }
   },
   "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": "50a35379",
   "metadata": {},
   "source": [
    "## Eksperyment 1 - global (ostrożny, rzadki)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "59c24a90",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-17T18:53:43.745123Z",
     "iopub.status.busy": "2026-08-17T18:53:43.745014Z",
     "iopub.status.idle": "2026-08-17T18:53:43.747406Z",
     "shell.execute_reply": "2026-08-17T18:53:43.747013Z"
    }
   },
   "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
}
