{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "06a22c91",
   "metadata": {},
   "source": [
    "# Python 作业任务"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "867cca5f",
   "metadata": {},
   "source": [
    "## 01. 成绩判断\n",
    "根据输入成绩输出：`优秀 / 良好 / 及格 / 不及格`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c687ae7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入成绩： 98\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "优秀\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入成绩：\"))\n",
    "\n",
    "if score >= 90:\n",
    "    print(\"优秀\")\n",
    "elif score >= 70:\n",
    "    print(\"良好\")\n",
    "elif score >= 60:\n",
    "    print(\"及格\")\n",
    "else:\n",
    "    print(\"不及格\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a35763d",
   "metadata": {},
   "source": [
    "## 02. 根据月份输出天数\n",
    "输入月份，返回该月天数；2 月可根据年份判断闰年。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "403e9256",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入月份(1-12)： 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31\n"
     ]
    }
   ],
   "source": [
    "month = int(input(\"请输入月份(1-12)：\"))\n",
    "\n",
    "if month in [1, 3, 5, 7, 8, 10, 12]:\n",
    "    print(31)\n",
    "elif month in [4, 6, 9, 11]:\n",
    "    print(30)\n",
    "elif month == 2:\n",
    "    year_text = input(\"请输入年份(可选，直接回车按平年28天)：\").strip()\n",
    "    if year_text:\n",
    "        year = int(year_text)\n",
    "        leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "        print(29 if leap else 28)\n",
    "    else:\n",
    "        print(28)\n",
    "else:\n",
    "    print(\"月份输入有误\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7107b711",
   "metadata": {},
   "source": [
    "## 03. 遍历列表筛选 5 的倍数\n",
    "已知 `ls1 = [2,3,5,75,32,4,15,23,55,20,9]`，筛选出 5 的倍数到新列表 `ls2`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4787d98d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 75, 15, 55, 20]\n"
     ]
    }
   ],
   "source": [
    "ls1 = [2, 3, 5, 75, 32, 4, 15, 23, 55, 20, 9]\n",
    "ls2 = []\n",
    "\n",
    "for x in ls1:\n",
    "    if x % 5 == 0:\n",
    "        ls2.append(x)\n",
    "\n",
    "print(ls2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ecc5d3a",
   "metadata": {},
   "source": [
    "## 04. 求 `1 + 2! + 3! + ... + 10!`\n",
    "使用 `math.factorial()` 计算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b217fef8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4037913\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "total = 1 + sum(math.factorial(i) for i in range(2, 11))\n",
    "print(total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6077cda0",
   "metadata": {},
   "source": [
    "## 05. 遍历字典分类及格与不及格\n",
    "将姓名分别放入两个列表并输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cbf6ccf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "及格名单: ['张三', '王五']\n",
      "不及格名单: ['李四', '赵六']\n"
     ]
    }
   ],
   "source": [
    "scores = {\"张三\": 95, \"李四\": 58, \"王五\": 82, \"赵六\": 44}\n",
    "pass_list = []\n",
    "fail_list = []\n",
    "\n",
    "for name, score in scores.items():\n",
    "    if score >= 60:\n",
    "        pass_list.append(name)\n",
    "    else:\n",
    "        fail_list.append(name)\n",
    "\n",
    "print(\"及格名单:\", pass_list)\n",
    "print(\"不及格名单:\", fail_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70b5e741",
   "metadata": {},
   "source": [
    "## 06. 持续输入正数并累加\n",
    "当输入负数或 0 停止；含异常处理，避免非法输入导致程序终止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0198bbe0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入一个正数(输入<=0结束)： 5\n",
      "请输入一个正数(输入<=0结束)： -5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终累加总和为: 5.0\n"
     ]
    }
   ],
   "source": [
    "total = 0\n",
    "\n",
    "while True:\n",
    "    try:\n",
    "        num = float(input(\"请输入一个正数(输入<=0结束)：\"))\n",
    "    except ValueError:\n",
    "        print(\"输入无效，请输入数字。\")\n",
    "        continue\n",
    "\n",
    "    if num <= 0:\n",
    "        break\n",
    "\n",
    "    total += num\n",
    "\n",
    "print(\"最终累加总和为:\", total)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
