{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6a7f496",
   "metadata": {},
   "source": [
    "# 16-23 · Валидация ввода\n",
    "\n",
    "Практика к разделу [«Валидация ввода и обратная связь»](../../site/chapters/glava-16/16-23-validatsiya-vvoda.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be7c2480",
   "metadata": {},
   "source": [
    "## Цель\n",
    "\n",
    "Проверить общий разбор числа и специализированную валидацию поверх него."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a10f9405",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:20.160493Z",
     "iopub.status.busy": "2026-08-18T21:45:20.160276Z",
     "iopub.status.idle": "2026-08-18T21:45:20.184614Z",
     "shell.execute_reply": "2026-08-18T21:45:20.184187Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False False True True\n",
      "-5.0 100.0 ''\n"
     ]
    }
   ],
   "source": [
    "def parse_number(text):\n",
    "    text = text.strip()\n",
    "    if not text:\n",
    "        return False, None, \"Поле не должно быть пустым\"\n",
    "    try:\n",
    "        return True, float(text), \"\"\n",
    "    except ValueError:\n",
    "        return False, None, \"Введите число\"\n",
    "\n",
    "ok1, val1, msg1 = parse_number(\"\")\n",
    "ok2, val2, msg2 = parse_number(\"abc\")\n",
    "ok3, val3, msg3 = parse_number(\"-5\")\n",
    "ok4, val4, msg4 = parse_number(\"100\")\n",
    "print(ok1, ok2, ok3, ok4)\n",
    "print(val3, val4, repr(msg4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35394ac2",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача — специализированная валидация"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7290bf31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T21:45:20.185830Z",
     "iopub.status.busy": "2026-08-18T21:45:20.185662Z",
     "iopub.status.idle": "2026-08-18T21:45:20.188919Z",
     "shell.execute_reply": "2026-08-18T21:45:20.188475Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False True False True\n"
     ]
    }
   ],
   "source": [
    "def validate_positive_amount(text):\n",
    "    ok, value, message = parse_number(text)\n",
    "    if not ok:\n",
    "        return False, message\n",
    "    if value <= 0:\n",
    "        return False, \"Число должно быть больше нуля\"\n",
    "    return True, \"\"\n",
    "\n",
    "def validate_positive_int(text):\n",
    "    ok, value, message = parse_number(text)\n",
    "    if not ok:\n",
    "        return False, message\n",
    "    if value != int(value):\n",
    "        return False, \"Введите целое число\"\n",
    "    if int(value) < 1:\n",
    "        return False, \"Число должно быть не меньше 1\"\n",
    "    return True, \"\"\n",
    "\n",
    "amount_negative_ok, _ = validate_positive_amount(\"-5\")\n",
    "amount_valid_ok, _ = validate_positive_amount(\"100\")\n",
    "people_float_ok, _ = validate_positive_int(\"2.5\")\n",
    "people_valid_ok, _ = validate_positive_int(\"5\")\n",
    "print(amount_negative_ok, amount_valid_ok, people_float_ok, people_valid_ok)"
   ]
  }
 ],
 "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
}
