{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "377f9005",
   "metadata": {},
   "source": [
    "# 第 4 天｜输入、类型转换与业务计算\n",
    "\n",
    "**日期：** 2026 年 7 月 23 日  \n",
    "**核心内容：** `input()`、`int()`、`float()`、算术运算、f-string 和 `.2f`\n",
    "\n",
    "科图题中的最小业务公式：\n",
    "\n",
    "```text\n",
    "销售总价 = 销售数量 × 商城价格\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "387ec89a",
   "metadata": {},
   "source": [
    "## `input()` 的返回值\n",
    "\n",
    "```python\n",
    "quantity_text = input(\"请输入销售数量：\")\n",
    "```\n",
    "\n",
    "`input()` 会：\n",
    "\n",
    "1. 显示提示文字；\n",
    "2. 等待键盘输入；\n",
    "3. 在按下回车后返回输入内容；\n",
    "4. 返回类型始终是 `str`。\n",
    "\n",
    "输入 `125` 得到的是 `\"125\"`，不能直接当作整数使用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4b90dfb",
   "metadata": {},
   "source": [
    "## 类型转换\n",
    "\n",
    "| 原始文本 | 业务含义 | 转换代码 | 转换结果 |\n",
    "|---|---|---|---|\n",
    "| `\"125\"` | 销售数量 | `int(\"125\")` | 整数 `125` |\n",
    "| `\"199.0\"` | 商城价格 | `float(\"199.0\")` | 小数 `199.0` |\n",
    "| `\"双肩包\"` | 商品名称 | 不转换 | 文本 |\n",
    "\n",
    "`int()` 适合整数文本，`float()` 适合可能带小数点的文本。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "644f6cb4",
   "metadata": {},
   "source": [
    "## 转换前后对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fc9f5a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "125 <class 'str'>\n",
      "125 <class 'int'>\n",
      "199.0 <class 'str'>\n",
      "199.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "quantity_text = \"125\"\n",
    "price_text = \"199.0\"\n",
    "\n",
    "quantity = int(quantity_text)\n",
    "price = float(price_text)\n",
    "\n",
    "print(quantity_text, type(quantity_text))\n",
    "print(quantity, type(quantity))\n",
    "print(price_text, type(price_text))\n",
    "print(price, type(price))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b94ab197",
   "metadata": {},
   "source": [
    "## 完整交互程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "197c8c0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_name = input(\"请输入商品名称：\")\n",
    "quantity_text = input(\"请输入销售数量：\")\n",
    "price_text = input(\"请输入商城价格：\")\n",
    "\n",
    "quantity = int(quantity_text)\n",
    "price = float(price_text)\n",
    "total_sales = quantity * price\n",
    "\n",
    "print(f\"商品：{product_name}\")\n",
    "print(f\"销售总价：{total_sales:.2f} 元\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7290fa32",
   "metadata": {},
   "source": [
    "## 代码解读\n",
    "\n",
    "| 步骤 | 示例值 | 类型 |\n",
    "|---|---|---|\n",
    "| 输入品名 | `\"双肩包\"` | `str` |\n",
    "| 输入数量 | `\"125\"` | `str` |\n",
    "| 输入价格 | `\"199.0\"` | `str` |\n",
    "| 转换数量 | `125` | `int` |\n",
    "| 转换价格 | `199.0` | `float` |\n",
    "| 计算销售总价 | `24875.0` | `float` |\n",
    "| 格式化输出 | `24875.00 元` | 显示文本 |\n",
    "\n",
    "先转换，再计算；不要把尚未转换的输入文本直接用于业务公式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bee6b5e",
   "metadata": {},
   "source": [
    "## f-string 与小数格式\n",
    "\n",
    "```python\n",
    "print(f\"销售总价：{total_sales:.2f} 元\")\n",
    "```\n",
    "\n",
    "| 部分 | 作用 |\n",
    "|---|---|\n",
    "| `f` | 允许字符串中插入变量 |\n",
    "| `{total_sales}` | 读取变量值 |\n",
    "| `:.2f` | 显示两位小数 |\n",
    "| `元` | 普通文本 |\n",
    "\n",
    "`.2f` 只控制显示，不改变原始业务公式。这个区别会在青娅题“保留两位小数、不足补 0”中再次出现。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450be2f4",
   "metadata": {},
   "source": [
    "## 业务例题\n",
    "\n",
    "| 商品 | 数量 | 价格 | 销售总价 |\n",
    "|---|---:|---:|---:|\n",
    "| 双肩包 | 125 | 199.0 | 24875.00 |\n",
    "| 帆布包 | 80 | 59.0 | 4720.00 |\n",
    "| 拉杆箱 | 60 | 399.0 | 23940.00 |\n",
    "\n",
    "手工计算一组结果，再用程序核对。真题中应先写清业务公式，再翻译成代码。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1357e15",
   "metadata": {},
   "source": [
    "## 常见转换错误\n",
    "\n",
    "| 输入 | 报错 | 原因 |\n",
    "|---|---|---|\n",
    "| 数量输入“一百二十五” | `ValueError` | 不是整数文本 |\n",
    "| 价格输入空白 | `ValueError` | 空字符串不能转浮点数 |\n",
    "| `int(\"199.5\")` | `ValueError` | 带小数点，应用 `float()` |\n",
    "| 删除 f-string 前的 `f` | 原样显示花括号 | 未启用变量替换 |\n",
    "\n",
    "后续使用 pandas 时，批量转换会采用 `pd.to_numeric(..., errors=\"coerce\")`，把非法值变为缺失值再处理。\n",
    "\n",
    "## 针对性练习\n",
    "\n",
    "完成 `学生练习.py` 中的输入、转换、计算和格式化，并使用三组给定数据核对结果。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "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"
  },
  "title": "第 4 天｜输入、转换与销售总价"
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
