{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cell-001",
   "metadata": {},
   "source": [
    "# Python with Princess Taiping\n",
    "\n",
    "跟太平公猪学 Python · Python กับเจ้าหญิงไท่ผิง\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 你好！我是太平公猪。不是公主，是公猪！我们一起用 Python 找一找，你的 Python 住在哪里吧！点我，就能听到我说话。\n",
    "\n",
    "> Hello! I'm Princess Taiping. My Chinese name is a joke: 太平公主 is Princess Taiping, and 太平公猪 sounds the same, but 公猪 means a boy pig! Let's use Python to find out where your Python lives! Tap me and you will hear me talk.\n",
    ">\n",
    "> สวัสดีจ้ะ! ครูชื่อเจ้าหญิงไท่ผิง ชื่อภาษาจีนของครูเป็นมุกตลก ฟังเหมือนคำว่าเจ้าหญิง แต่แปลว่าหมูตัวผู้! มาใช้ Python หากันว่า Python ของหนูอยู่ที่ไหน! แตะที่ครูแล้วจะได้ยินครูพูด\n",
    "\n",
    "Click a grey cell and press **Shift + Enter** to run it. Run the cells in order, from the top. Change things and run them again: experimenting is how you learn. The answers to every challenge are on the page.\n",
    "\n",
    "点一个灰色的格子，按 **Shift + Enter** 运行它。从上到下，按顺序运行这些格子。改一改，再运行一次：动手试才学得会。每个挑战的答案都在网页上。\n",
    "\n",
    "คลิกเซลล์สีเทาแล้วกด **Shift + Enter** เพื่อรันมัน รันเซลล์ตามลำดับ จากบนลงล่าง ลองเปลี่ยนแล้วรันอีกครั้ง: การลองเล่นคือวิธีเรียนรู้ คำตอบของทุกโจทย์อยู่ในหน้าเว็บ\n",
    "\n",
    "The page: https://krueng.ai/python_piggy.html"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-002",
   "metadata": {},
   "source": [
    "## Step 1 · Variables: names that remember\n",
    "\n",
    "变量：会记住东西的名字 · ตัวแปร: ชื่อที่จำได้\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第一步：变量。变量就是一个名字，它会记住一个值。运行下面的格子，看看你的 Python 住在哪里。\n",
    "\n",
    "> Step one: variables. A variable is a name that remembers a value. Run the cells below and see where your Python lives.\n",
    ">\n",
    "> ขั้นที่หนึ่ง: ตัวแปร ตัวแปรคือชื่อที่จำค่าหนึ่งค่าไว้ รันเซลล์ข้างล่าง แล้วดูว่า Python ของหนูอยู่ที่ไหน"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-003",
   "metadata": {},
   "source": [
    "# Step 1: variables. A variable is a name for a value.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-004",
   "metadata": {},
   "source": [
    "pig = \"Princess Taiping\"              # text in quotes is a string\n",
    "age = 3                            # a whole number is an int\n",
    "print(pig, \"is\", age, \"years old\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-005",
   "metadata": {},
   "source": [
    "age = age + 1                      # a birthday: now the name age holds 4\n",
    "print(pig, \"is now\", age)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-006",
   "metadata": {},
   "source": [
    "version = platform.python_version()    # which Python is running this\n",
    "machine = platform.machine()           # the kind of computer it runs on\n",
    "place = sys.platform                   # where it runs: emscripten is a web browser\n",
    "print(\"Python\", version)\n",
    "print(\"Machine:\", machine)\n",
    "print(\"Platform:\", place)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-007",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Make a variable called `my_name` that holds your name in quotes, and one called `my_age` that holds your age. Print them both. Then give yourself a birthday: add 1 to `my_age` and print it again.\n",
    "\n",
    "建一个叫 `my_name` 的变量，用引号装上你的名字，再建一个叫 `my_age` 的变量，装上你的年龄。把它们都打印出来。然后给自己过个生日：给 `my_age` 加 1，再打印一次。\n",
    "\n",
    "สร้างตัวแปรชื่อ `my_name` ที่เก็บชื่อของคุณไว้ในเครื่องหมายคำพูด และอีกตัวชื่อ `my_age` ที่เก็บอายุของคุณ พิมพ์ทั้งสองตัวออกมา แล้วจัดวันเกิดให้ตัวเอง: บวก 1 เข้าไปใน `my_age` แล้วพิมพ์อีกครั้ง"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-008",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-009",
   "metadata": {},
   "source": [
    "## Step 2 · Lists: many things under one name\n",
    "\n",
    "列表：一个名字装很多东西 · ลิสต์: หลายอย่างในชื่อเดียว\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第二步：列表。列表把很多东西排成一队。我们来看看 Python 自带了哪些模块！\n",
    "\n",
    "> Step two: lists. A list puts many things in a line. Let's see which modules come with Python!\n",
    ">\n",
    "> ขั้นที่สอง: ลิสต์ ลิสต์เอาหลาย ๆ อย่างมาเรียงต่อกันเป็นแถว มาดูกันว่า Python มีโมดูลอะไรมาให้บ้าง!"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-010",
   "metadata": {},
   "source": [
    "# Step 2: lists. A list keeps many values in order, under one name.\n",
    "import sys"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-011",
   "metadata": {},
   "source": [
    "snacks = [\"mango\", \"sticky rice\", \"khao soi\"]\n",
    "print(snacks[0])                   # Python counts from 0, so [0] is the first\n",
    "print(len(snacks), \"snacks\")       # len() counts what is in the list\n",
    "snacks.append(\"roti\")              # append() adds one more at the end\n",
    "print(snacks)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-012",
   "metadata": {},
   "source": [
    "modules = sorted(sys.stdlib_module_names)   # every module that comes with Python, A to Z\n",
    "print(len(modules), \"modules come with Python\")\n",
    "print(\"First:\", modules[0])\n",
    "print(\"Last:\", modules[-1])        # [-1] is the last one"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-013",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Add your favourite snack to the list with `append()`, then print the list and its length. Then count the modules that are built right into Python itself: `len(sys.builtin_module_names)`. How many are there?\n",
    "\n",
    "用 `append()` 把你最喜欢的零食加进列表，然后打印列表和它的长度。再数一数直接装在 Python 身体里的模块：`len(sys.builtin_module_names)`。一共有多少个？\n",
    "\n",
    "เพิ่มขนมที่คุณชอบลงในลิสต์ด้วย `append()` แล้วพิมพ์ลิสต์และความยาวของมัน จากนั้นนับโมดูลที่ฝังอยู่ในตัว Python เลย: `len(sys.builtin_module_names)` มีทั้งหมดกี่โมดูล?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-014",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-015",
   "metadata": {},
   "source": [
    "## Step 3 · Ask the computer: platform\n",
    "\n",
    "问问电脑：platform · ถามคอมพิวเตอร์: platform\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第三步：问问电脑！`platform` 会告诉你，你的 Python 在什么样的电脑上运行。\n",
    "\n",
    "> Step three: ask the computer! `platform` tells you what kind of computer your Python runs on.\n",
    ">\n",
    "> ขั้นที่สาม: ถามคอมพิวเตอร์ดู! `platform` บอกหนูว่า Python ของหนูรันอยู่บนคอมพิวเตอร์แบบไหน"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-016",
   "metadata": {},
   "source": [
    "# Step 3: ask the computer. platform tells you what your Python is running on.\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-017",
   "metadata": {},
   "source": [
    "print(\"Platform:\", platform.platform())    # everything in one line\n",
    "print(\"System:\", platform.system())        # Emscripten is a web browser, Darwin is a Mac\n",
    "print(\"Machine:\", platform.machine())      # wasm32 is WebAssembly, arm64 is an Apple chip\n",
    "bits = platform.architecture()             # a pair of answers, like a list of two\n",
    "print(\"Bits:\", bits[0])                    # [0] is the first answer\n",
    "print(\"Python:\", platform.python_implementation(), platform.python_version())"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-018",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Print `sys.version`, Python's whole version line, and `platform.node()`, the name of the computer. You need `import sys` for the first. What do they say in a web browser?\n",
    "\n",
    "打印 `sys.version`，也就是 Python 完整的版本那一行，再打印 `platform.node()`，也就是电脑的名字。第一个要先写 `import sys`。在网页浏览器里，它们说了什么？\n",
    "\n",
    "พิมพ์ `sys.version` ซึ่งเป็นบรรทัดรุ่นทั้งหมดของ Python และ `platform.node()` ซึ่งเป็นชื่อของคอมพิวเตอร์ อันแรกต้องใช้ `import sys` ด้วย ในเว็บเบราว์เซอร์มันบอกว่าอะไร?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-019",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-020",
   "metadata": {},
   "source": [
    "## Step 4 · Dictionaries: find things by name\n",
    "\n",
    "字典：用名字找东西 · ดิกชันนารี: หาของด้วยชื่อ\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第四步：字典。字典用名字来找东西，就像真正的字典用一个词找到它的意思。\n",
    "\n",
    "> Step four: dictionaries. A dictionary finds things by a name, the way a real dictionary finds the meaning of a word.\n",
    ">\n",
    "> ขั้นที่สี่: ดิกชันนารี ดิกชันนารีหาของด้วยชื่อ เหมือนพจนานุกรมจริง ๆ ที่หาความหมายของคำ"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-021",
   "metadata": {},
   "source": [
    "# Step 4: dictionaries. A dictionary finds a value by its name, called a key.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-022",
   "metadata": {},
   "source": [
    "pig = {\"name\": \"Princess Taiping\", \"food\": \"mango\", \"legs\": 4}\n",
    "print(pig[\"food\"])                 # look up the key \"food\"\n",
    "pig[\"colour\"] = \"pink\"             # a new key, and its value\n",
    "print(pig)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-023",
   "metadata": {},
   "source": [
    "# sys.modules is a dictionary: the name of every module Python has opened\n",
    "print(len(sys.modules), \"modules are open\")\n",
    "print(sys.modules[\"platform\"])     # look one up by its name: it says where it lives"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-024",
   "metadata": {},
   "source": [
    "python = {}                        # an empty dictionary, to fill in\n",
    "python[\"version\"] = platform.python_version()\n",
    "python[\"system\"] = platform.system()\n",
    "python[\"machine\"] = platform.machine()\n",
    "print(python)\n",
    "print(\"This is Python\", python[\"version\"], \"on\", python[\"system\"])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-025",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Add the key `\"best friend\"` to the `pig` dictionary, with your own name as its value, and print the dictionary. Then print `sys.modules[\"sys\"]`. What does `sys` say about itself?\n",
    "\n",
    "给 `pig` 字典加上键 `\"best friend\"`，用你自己的名字当它的值，然后把字典打印出来。再打印 `sys.modules[\"sys\"]`。`sys` 是怎么说它自己的？\n",
    "\n",
    "เพิ่มคีย์ `\"best friend\"` ลงในดิกชันนารี `pig` ใช้ชื่อของคุณเป็นค่า แล้วพิมพ์ดิกชันนารีออกมา จากนั้นพิมพ์ `sys.modules[\"sys\"]` `sys` พูดถึงตัวเองว่าอย่างไร?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-026",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-027",
   "metadata": {},
   "source": [
    "## Step 5 · For loops: again and again\n",
    "\n",
    "for 循环：一遍又一遍 · ลูป for: ซ้ำแล้วซ้ำอีก\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第五步：for 循环。循环让电脑把同一件事做很多遍。电脑永远不会累！\n",
    "\n",
    "> Step five: for loops. A loop makes the computer do the same thing many times. Computers never get tired!\n",
    ">\n",
    "> ขั้นที่ห้า: ลูป for ลูปทำให้คอมพิวเตอร์ทำสิ่งเดิมซ้ำหลาย ๆ ครั้ง คอมพิวเตอร์ไม่มีวันเหนื่อย!"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-028",
   "metadata": {},
   "source": [
    "# Step 5: for loops. Do the same thing for every item, one at a time.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-029",
   "metadata": {},
   "source": [
    "for snack in [\"mango\", \"sticky rice\", \"khao soi\"]:\n",
    "    print(\"Taiping eats\", snack)     # the lines pushed in are what repeats"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-030",
   "metadata": {},
   "source": [
    "for name in sys.builtin_module_names:   # the modules built into Python itself\n",
    "    print(\"🧰\", name)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-031",
   "metadata": {},
   "source": [
    "python = {\"version\": platform.python_version(), \"system\": platform.system(),\n",
    "          \"machine\": platform.machine(), \"bits\": platform.architecture()[0]}\n",
    "for key in python:                 # a loop over a dictionary gives its keys\n",
    "    print(key, \"=\", python[key])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-032",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Write a loop over your snacks that prints each one in capital letters with `snack.upper()`. Then count: make `count = 0` before a loop over `sys.builtin_module_names`, add 1 to it inside the loop, and print `count` after. Is it the same as `len(sys.builtin_module_names)`?\n",
    "\n",
    "写一个循环，用 `snack.upper()` 把你的零食一个一个用大写字母打印出来。然后数一数：在对 `sys.builtin_module_names` 的循环前面写 `count = 0`，在循环里给它加 1，循环结束后打印 `count`。它和 `len(sys.builtin_module_names)` 一样吗？\n",
    "\n",
    "เขียนลูปที่วนขนมของคุณ แล้วพิมพ์แต่ละอย่างเป็นตัวพิมพ์ใหญ่ด้วย `snack.upper()` จากนั้นลองนับ: ตั้ง `count = 0` ก่อนลูปที่วน `sys.builtin_module_names` บวก 1 เข้าไปในลูป แล้วพิมพ์ `count` หลังลูป มันเท่ากับ `len(sys.builtin_module_names)` ไหม?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-033",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-034",
   "metadata": {},
   "source": [
    "## Step 6 · Booleans: True or False, and if\n",
    "\n",
    "布尔值：True 还是 False，还有 if · บูลีน: True หรือ False และ if\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第六步：布尔值。答案只有两个：`True`（真）或者 `False`（假）。`if` 会根据答案做选择。你的 Python 在网页浏览器里吗？\n",
    "\n",
    "> Step six: booleans. There are only two answers: `True` or `False`. `if` makes a choice from the answer. Is your Python inside a web browser?\n",
    ">\n",
    "> ขั้นที่หก: บูลีน มีคำตอบแค่สองแบบ: `True` (จริง) หรือ `False` (เท็จ) `if` เลือกว่าจะทำอะไรจากคำตอบ Python ของหนูอยู่ในเว็บเบราว์เซอร์หรือเปล่า?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-035",
   "metadata": {},
   "source": [
    "# Step 6: booleans. A boolean is True or False, and \"if\" picks what to do.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-036",
   "metadata": {},
   "source": [
    "print(5 > 3)                               # True\n",
    "print(\"roti\" in [\"mango\", \"roti\"])         # \"in\" asks: is it in there? True"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-037",
   "metadata": {},
   "source": [
    "print(\"statistics\" in sys.modules)         # has Python opened statistics yet?\n",
    "import statistics\n",
    "print(\"statistics\" in sys.modules)         # it has now"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-038",
   "metadata": {},
   "source": [
    "in_browser = \"wasm\" in platform.machine()  # wasm is in the name inside a web browser\n",
    "if in_browser:\n",
    "    print(\"🌐 Python is running inside your web browser!\")\n",
    "else:\n",
    "    print(\"💻 Python is running on the computer itself.\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-039",
   "metadata": {},
   "source": [
    "helpers = 0\n",
    "for name in sys.builtin_module_names:\n",
    "    if name.startswith(\"_\"):               # True for one of Python's own helpers\n",
    "        helpers = helpers + 1\n",
    "print(helpers, \"of the\", len(sys.builtin_module_names), \"built-in modules start with _\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-040",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Does a module called `random` come with Python? Make `has_random = \"random\" in sys.stdlib_module_names` and print it. Then write an `if` that prints `🎲 random comes with Python!` when it is `True`.\n",
    "\n",
    "Python 自带一个叫 `random` 的模块吗？写 `has_random = \"random\" in sys.stdlib_module_names`，然后把它打印出来。再写一个 `if`，在它是 `True` 的时候打印 `🎲 random comes with Python!`。\n",
    "\n",
    "Python มีโมดูลชื่อ `random` มาให้ไหม? สร้าง `has_random = \"random\" in sys.stdlib_module_names` แล้วพิมพ์ออกมา จากนั้นเขียน `if` ที่พิมพ์ `🎲 random comes with Python!` เมื่อมันเป็น `True`"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-041",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-042",
   "metadata": {},
   "source": [
    "## Step 7 · Comparing: is it the same?\n",
    "\n",
    "比较：一样吗？ · การเปรียบเทียบ: เหมือนกันไหม?\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第七步：比较。两个等号 `==` 是问“一样吗？”，一个等号 `=` 是给东西起名字。可别弄混了！\n",
    "\n",
    "> Step seven: comparing. Two equals signs `==` ask \"are they the same?\" One equals sign `=` gives a name. Don't mix them up!\n",
    ">\n",
    "> ขั้นที่เจ็ด: การเปรียบเทียบ เครื่องหมายเท่ากับสองตัว `==` ถามว่า \"เหมือนกันไหม?\" เครื่องหมายเท่ากับตัวเดียว `=` ใช้ตั้งชื่อ อย่าสับสนนะ!"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-043",
   "metadata": {},
   "source": [
    "# Step 7: comparing. == asks \"are these the same?\" One = only gives a name.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-044",
   "metadata": {},
   "source": [
    "print(2 + 2 == 4)                  # True\n",
    "print(\"Taiping\" == \"taiping\")          # False: a capital letter makes it different\n",
    "print(\"15\" == 15)                  # False: text is not a number\n",
    "print(int(\"15\") == 15)             # True: int() turns the text into a number\n",
    "print(\"mango\" != \"roti\")           # != asks \"are these different?\" True"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-045",
   "metadata": {},
   "source": [
    "if sys.platform == \"emscripten\":\n",
    "    print(\"🌐 A web browser\")\n",
    "elif sys.platform == \"darwin\":     # elif means \"else, if this is True\"\n",
    "    print(\"🍏 A Mac\")\n",
    "elif sys.platform == \"win32\":\n",
    "    print(\"🪟 Windows\")\n",
    "else:\n",
    "    print(\"🐧 Something else:\", sys.platform)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-046",
   "metadata": {},
   "source": [
    "version = platform.python_version()   # for example \"3.14.2\"\n",
    "minor = version.split(\".\")[1]         # the part after the first dot, \"14\"\n",
    "if minor == \"14\":\n",
    "    print(\"Python 3.14: say it pi-thon, because π is 3.14...\")\n",
    "elif minor == \"13\":\n",
    "    print(\"Python 3.13\")\n",
    "else:\n",
    "    print(\"Python\", version)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-047",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Print `\"Mango\" == \"mango\"`, then `\"Mango\".lower() == \"mango\"`. Why is the second one `True`? Then write an `if` that prints `Same!` when `len(\"mango\") == 5`.\n",
    "\n",
    "打印 `\"Mango\" == \"mango\"`，再打印 `\"Mango\".lower() == \"mango\"`。为什么第二个是 `True`？然后写一个 `if`，在 `len(\"mango\") == 5` 的时候打印 `Same!`。\n",
    "\n",
    "พิมพ์ `\"Mango\" == \"mango\"` แล้วพิมพ์ `\"Mango\".lower() == \"mango\"` ทำไมอันที่สองถึงเป็น `True`? จากนั้นเขียน `if` ที่พิมพ์ `Same!` เมื่อ `len(\"mango\") == 5`"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-048",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-049",
   "metadata": {},
   "source": [
    "## Step 8 · Functions: give your code a name\n",
    "\n",
    "函数：给你的代码起个名字 · ฟังก์ชัน: ตั้งชื่อให้โค้ดของคุณ\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第八步：函数。函数就是给几行代码起一个名字。以后只要叫它的名字就行了。\n",
    "\n",
    "> Step eight: functions. A function gives a few lines of code a name. After that, you only need to call its name.\n",
    ">\n",
    "> ขั้นที่แปด: ฟังก์ชัน ฟังก์ชันคือการตั้งชื่อให้โค้ดไม่กี่บรรทัด หลังจากนั้น หนูแค่เรียกชื่อมันก็พอ"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-050",
   "metadata": {},
   "source": [
    "# Step 8: functions. Give a few lines a name, then use them again and again.\n",
    "import sys"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-051",
   "metadata": {},
   "source": [
    "def greet(name):\n",
    "    print(\"你好 \" + name + \"! I am Princess Taiping.\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-052",
   "metadata": {},
   "source": [
    "def is_open(module):\n",
    "    return module in sys.modules   # return hands the answer back"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-053",
   "metadata": {},
   "source": [
    "def digits(number):\n",
    "    return len(str(number))        # str() turns the number into text, to count its digits"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-054",
   "metadata": {},
   "source": [
    "greet(\"Mali\")\n",
    "greet(\"Xiaoming\")\n",
    "print(\"json is open:\", is_open(\"json\"))\n",
    "print(\"turtle is open:\", is_open(\"turtle\"))\n",
    "print(\"Biggest count:\", sys.maxsize)       # the biggest number this Python counts with\n",
    "print(\"That has\", digits(sys.maxsize), \"digits\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-055",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Write a function `double(number)` that returns `number * 2`, and print `double(21)`. Then write a function `bits()` that returns `platform.architecture()[0]`, and print what it says.\n",
    "\n",
    "写一个函数 `double(number)`，让它交回 `number * 2`，然后打印 `double(21)`。再写一个函数 `bits()`，让它交回 `platform.architecture()[0]`，把它说的打印出来。\n",
    "\n",
    "เขียนฟังก์ชัน `double(number)` ที่ส่งกลับ `number * 2` แล้วพิมพ์ `double(21)` จากนั้นเขียนฟังก์ชัน `bits()` ที่ส่งกลับ `platform.architecture()[0]` แล้วพิมพ์สิ่งที่มันบอก"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-056",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-057",
   "metadata": {},
   "source": [
    "## Step 9 · A report card for your Python\n",
    "\n",
    "给你的 Python 做一张成绩单 · สมุดพกของ Python ของคุณ\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 第九步：把学过的东西都放在一起，给你的 Python 做一张成绩单！\n",
    "\n",
    "> Step nine: put everything you learned together, and make a report card for your Python!\n",
    ">\n",
    "> ขั้นที่เก้า: เอาทุกอย่างที่เรียนมารวมกัน แล้วทำสมุดพกให้ Python ของหนู!"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-058",
   "metadata": {},
   "source": [
    "# Step 9: Princess Taiping's report card for your Python. Steps 1 to 8, all together.\n",
    "import sys\n",
    "import platform"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-059",
   "metadata": {},
   "source": [
    "def is_open(module):\n",
    "    return module in sys.modules"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-060",
   "metadata": {},
   "source": [
    "def report():\n",
    "    card = {}\n",
    "    card[\"Python\"] = platform.python_version()\n",
    "    card[\"system\"] = platform.system()\n",
    "    card[\"machine\"] = platform.machine()\n",
    "    card[\"bits\"] = platform.architecture()[0]\n",
    "    card[\"in a browser\"] = sys.platform == \"emscripten\"\n",
    "    card[\"modules open\"] = len(sys.modules)\n",
    "    card[\"built-in modules\"] = len(sys.builtin_module_names)\n",
    "    card[\"IPython open\"] = is_open(\"IPython\")\n",
    "    return card"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-061",
   "metadata": {},
   "source": [
    "card = report()\n",
    "print(\"🐷 Princess Taiping's report card\")\n",
    "for key in card:\n",
    "    print(\"   \", key, \":\", card[key])"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-062",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Add one more line to the card inside `report()`: `card[\"modules with Python\"] = len(sys.stdlib_module_names)`. Then add a line of your own. What else could you ask `sys` or `platform`?\n",
    "\n",
    "在 `report()` 里给成绩单再加一行：`card[\"modules with Python\"] = len(sys.stdlib_module_names)`。然后加一行你自己想的。你还能问 `sys` 或者 `platform` 什么？\n",
    "\n",
    "เพิ่มอีกหนึ่งบรรทัดในสมุดพก ข้างใน `report()`: `card[\"modules with Python\"] = len(sys.stdlib_module_names)` แล้วเพิ่มบรรทัดของคุณเอง คุณจะถามอะไร `sys` หรือ `platform` ได้อีกบ้าง?"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-063",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-064",
   "metadata": {},
   "source": [
    "## Step 10 · Make the browser talk\n",
    "\n",
    "让浏览器说话 · ทำให้เบราว์เซอร์พูด\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 最后一步：让你的浏览器说中文，就像我一样！先把声音打开，再运行。\n",
    "\n",
    "> Last step: make your browser speak Chinese, just like me! Turn the sound on first, then run it.\n",
    ">\n",
    "> ขั้นสุดท้าย: ทำให้เบราว์เซอร์ของหนูพูดภาษาจีน เหมือนครูเลย! เปิดเสียงก่อน แล้วค่อยรัน"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-065",
   "metadata": {},
   "source": [
    "# Step 10: make the browser talk, the way Princess Taiping does.\n",
    "from IPython.display import Javascript, display"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-066",
   "metadata": {},
   "source": [
    "def say(words, language):\n",
    "    # JavaScript, the browser's own language: find a voice for the language, then speak\n",
    "    script = \"\"\"\n",
    "    function talk() {\n",
    "        const voices = speechSynthesis.getVoices().filter(v => v.lang.startsWith(\"LANGUAGE\"));\n",
    "        if (voices.length == 0) {\n",
    "            element.textContent = \"This browser has no voice for LANGUAGE.\";\n",
    "            return;\n",
    "        }\n",
    "        const line = new SpeechSynthesisUtterance(\"WORDS\");\n",
    "        line.voice = voices.find(v => v.name.startsWith(\"Google\")) || voices[0];\n",
    "        line.lang = line.voice.lang;\n",
    "        speechSynthesis.speak(line);\n",
    "    }\n",
    "    if (speechSynthesis.getVoices().length > 0) {\n",
    "        talk();\n",
    "    } else {\n",
    "        speechSynthesis.addEventListener(\"voiceschanged\", talk, {once: true});\n",
    "    }\n",
    "    \"\"\"\n",
    "    script = script.replace(\"WORDS\", words).replace(\"LANGUAGE\", language)\n",
    "    display(Javascript(script))    # Jupyter hands the JavaScript to the browser\n",
    "    print(\"The browser was asked to say:\", words)"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-067",
   "metadata": {},
   "source": [
    "say(\"你好！我是太平公猪。\", \"zh\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-068",
   "metadata": {},
   "source": [
    "### ✏️ Try it · 试一试 · ลองดู\n",
    "\n",
    "Make the browser say your name in English: `say(\"Hello Mali\", \"en\")`, with your name instead of Mali. Then try Thai, with `\"th\"`: `say(\"สวัสดีค่ะ เจ้าหญิงไท่ผิง\", \"th\")`. Can you hear a different voice? If the browser has no Thai voice, it tells you so under the cell.\n",
    "\n",
    "让浏览器用英语说出你的名字：`say(\"Hello Mali\", \"en\")`，把 Mali 换成你的名字。再用 `\"th\"` 试试泰语：`say(\"สวัสดีค่ะ เจ้าหญิงไท่ผิง\", \"th\")`。你听到不一样的声音了吗？如果浏览器没有泰语的声音，它会在格子下面告诉你。\n",
    "\n",
    "ทำให้เบราว์เซอร์พูดชื่อของคุณเป็นภาษาอังกฤษ: `say(\"Hello Mali\", \"en\")` โดยใส่ชื่อของคุณแทน Mali จากนั้นลองภาษาไทย ด้วย `\"th\"`: `say(\"สวัสดีค่ะ เจ้าหญิงไท่ผิง\", \"th\")` ได้ยินเสียงที่ต่างกันไหม? ถ้าเบราว์เซอร์ไม่มีเสียงภาษาไทย มันจะบอกคุณไว้ใต้เซลล์"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-069",
   "metadata": {},
   "source": [
    "# Try it here · 在这里试一试 · ลองที่นี่"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-070",
   "metadata": {},
   "source": [
    "## 🧩 Puzzles: be a Python detective\n",
    "\n",
    "谜题：做个 Python 小侦探 · ปริศนา: เป็นนักสืบ Python\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 解谜时间到了！用你学到的所有本领，解开这五道谜题。先自己试一试，再看答案。\n",
    "\n",
    "> Puzzle time! Use everything you have learned to solve these five puzzles. Try them yourself before you look at the answers.\n",
    ">\n",
    "> ได้เวลาไขปริศนาแล้ว! ใช้ทุกอย่างที่หนูเรียนมา ไขปริศนาทั้งห้าข้อนี้ ลองทำเองก่อนแล้วค่อยดูคำตอบนะ"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cell-071",
   "metadata": {},
   "source": [
    "### Puzzle 1 · Princess Taiping's secret message\n",
    "\n",
    "太平公猪的秘密消息 · ข้อความลับของเจ้าหญิงไท่ผิง\n",
    "\n",
    "Princess Taiping hid a word in this list. Take the first letter of every word and join them all together. What word is it? Hint: `word[0]` is the first letter of a word, and `+` joins two pieces of text: `\"P\" + \"Y\"` is `\"PY\"`.\n",
    "\n",
    "太平公猪在这个列表里藏了一个词。把每个单词的第一个字母拿出来，连在一起。是什么词？提示：`word[0]` 是一个单词的第一个字母，`+` 能把两段文字连起来：`\"P\" + \"Y\"` 就是 `\"PY\"`。\n",
    "\n",
    "เจ้าหญิงไท่ผิงซ่อนคำหนึ่งไว้ในลิสต์นี้ เอาตัวอักษรแรกของทุกคำมาต่อกัน มันคือคำว่าอะไร? คำใบ้: `word[0]` คือตัวอักษรแรกของคำ และ `+` ต่อข้อความสองชิ้นเข้าด้วยกัน: `\"P\" + \"Y\"` คือ `\"PY\"`"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-072",
   "metadata": {},
   "source": [
    "# Puzzle 1: Princess Taiping's secret message.\n",
    "words = [\"Pigs\", \"Yell\", \"Tremendously\", \"Happy\", \"Old\", \"Noises\"]"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-073",
   "metadata": {},
   "source": [
    "# Your answer here · 你的答案 · คำตอบของคุณ"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-074",
   "metadata": {},
   "source": [
    "### Puzzle 2 · The longest name\n",
    "\n",
    "最长的名字 · ชื่อที่ยาวที่สุด\n",
    "\n",
    "Which module that comes with Python has the longest name? Go through `sorted(sys.stdlib_module_names)`, and print the longest name and how many letters it has. Hint: keep the longest name so far in a variable, and compare each name with it using `len()` and `>`.\n",
    "\n",
    "Python 自带的模块里，哪一个的名字最长？走过 `sorted(sys.stdlib_module_names)`，把最长的名字打印出来，再说出它有几个字母。提示：用一个变量记住到目前为止最长的名字，再用 `len()` 和 `>` 把每个名字和它比一比。\n",
    "\n",
    "โมดูลไหนที่มากับ Python มีชื่อยาวที่สุด? ไล่ไปตาม `sorted(sys.stdlib_module_names)` แล้วพิมพ์ชื่อที่ยาวที่สุด พร้อมบอกว่ามีกี่ตัวอักษร คำใบ้: เก็บชื่อที่ยาวที่สุดจนถึงตอนนี้ไว้ในตัวแปร แล้วเปรียบเทียบแต่ละชื่อกับมันด้วย `len()` และ `>`"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-075",
   "metadata": {},
   "source": [
    "# Your answer here · 你的答案 · คำตอบของคุณ"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-076",
   "metadata": {},
   "source": [
    "### Puzzle 3 · Python points\n",
    "\n",
    "Python 的分数 · คะแนนของ Python\n",
    "\n",
    "Write a function `python_points()` that gives your Python one point for each of these: 64 bits, a version after 3.12, `json` open, and more than 50 built-in modules. Return the points, print them, and print a ⭐ for each point. Hint: `sys.version_info.minor` is the middle number of the version, as a number, and `\"⭐\" * 3` is `\"⭐⭐⭐\"`.\n",
    "\n",
    "写一个函数 `python_points()`，下面每一项给你的 Python 加一分：用 64 位、版本在 3.12 以后、`json` 是打开的、身体里装着多于 50 个模块。把分数交回来，打印出来，再每一分打印一颗 ⭐。提示：`sys.version_info.minor` 是版本中间的那个数，是数字，`\"⭐\" * 3` 就是 `\"⭐⭐⭐\"`。\n",
    "\n",
    "เขียนฟังก์ชัน `python_points()` ที่ให้ Python ของคุณหนึ่งคะแนนสำหรับแต่ละข้อต่อไปนี้: ใช้ 64 บิต รุ่นหลังจาก 3.12 `json` เปิดอยู่ และมีโมดูลฝังอยู่ในตัวมากกว่า 50 โมดูล ส่งคะแนนกลับมา พิมพ์ออกมา และพิมพ์ ⭐ หนึ่งดวงต่อหนึ่งคะแนน คำใบ้: `sys.version_info.minor` คือเลขตรงกลางของรุ่น ในแบบตัวเลข และ `\"⭐\" * 3` คือ `\"⭐⭐⭐\"`"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-077",
   "metadata": {},
   "source": [
    "# Your answer here · 你的答案 · คำตอบของคุณ"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-078",
   "metadata": {},
   "source": [
    "### Puzzle 4 · The treasure hunt\n",
    "\n",
    "寻宝 · ล่าขุมทรัพย์\n",
    "\n",
    "Princess Taiping hid a mango. Each place in `clues` says where to look next. Start at the `\"door\"` and follow the clues until you find the `\"mango\"`. Print each place you look, and how many steps it took. Hint: a loop over `clues` goes round once for each clue, which is enough; use `!=` to stop moving once you have found the mango.\n",
    "\n",
    "太平公猪藏了一个芒果。`clues` 里的每个地方都告诉你下一步去哪里找。从 `\"door\"` 出发，跟着线索走，直到找到 `\"mango\"`。把你找过的每个地方打印出来，再说出用了几步。提示：对 `clues` 做循环，每条线索转一圈，刚好够用；用 `!=` 在找到芒果以后停下来。\n",
    "\n",
    "เจ้าหญิงไท่ผิงซ่อนมะม่วงไว้ แต่ละที่ใน `clues` บอกว่าต้องไปหาต่อที่ไหน เริ่มที่ `\"door\"` แล้วตามคำใบ้ไปจนเจอ `\"mango\"` พิมพ์ทุกที่ที่คุณไปหา และบอกว่าใช้กี่ก้าว คำใบ้: ลูปที่วน `clues` จะวนหนึ่งรอบต่อหนึ่งคำใบ้ ซึ่งพอดี ใช้ `!=` เพื่อหยุดเดินเมื่อเจอมะม่วงแล้ว"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-079",
   "metadata": {},
   "source": [
    "# Puzzle 4: the treasure hunt. Each place says where to look next.\n",
    "clues = {\"door\": \"sofa\", \"sofa\": \"lamp\", \"lamp\": \"garden\",\n",
    "         \"garden\": \"pond\", \"pond\": \"mango\"}"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cell-080",
   "metadata": {},
   "source": [
    "# Your answer here · 你的答案 · คำตอบของคุณ"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-081",
   "metadata": {},
   "source": [
    "### Puzzle 5 · Letter count\n",
    "\n",
    "数字母 · นับตัวอักษร\n",
    "\n",
    "Which letter starts the most modules that come with Python? Leave out the names that start with `_`. Count them in a dictionary: the letter is the key, how many is the value. Then find the letter with the biggest count. Hint: `\"G\" in counts` asks whether the key `\"G\"` is in the dictionary yet, and `name[0].upper()` is the first letter as a capital.\n",
    "\n",
    "Python 自带的模块里，哪个字母开头的最多？以 `_` 开头的名字不算。用一个字典来数：字母是键，个数是值。然后找出个数最多的那个字母。提示：`\"G\" in counts` 问的是字典里有没有键 `\"G\"`，`name[0].upper()` 是第一个字母的大写。\n",
    "\n",
    "ตัวอักษรไหนเป็นตัวแรกของชื่อโมดูลที่มากับ Python มากที่สุด? ไม่นับชื่อที่ขึ้นต้นด้วย `_` นับในดิกชันนารี: ตัวอักษรเป็นคีย์ จำนวนเป็นค่า แล้วหาตัวอักษรที่นับได้มากที่สุด คำใบ้: `\"G\" in counts` ถามว่ามีคีย์ `\"G\"` ในดิกชันนารีแล้วหรือยัง และ `name[0].upper()` คือตัวอักษรแรกแบบตัวพิมพ์ใหญ่"
   ]
  },
  {
   "cell_type": "code",
   "id": "cell-082",
   "metadata": {},
   "source": [
    "# Your answer here · 你的答案 · คำตอบของคุณ"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "cell-083",
   "metadata": {},
   "source": [
    "## ⭐ What you learned\n",
    "\n",
    "🐷 **太平公猪 Princess Taiping:** 你真棒！你学会了七个 Python 的本领，还知道了你的 Python 住在哪里。下次见！\n",
    "\n",
    "> Well done! You learned seven good Python ideas, and you found out where your Python lives. See you next time!\n",
    ">\n",
    "> เก่งมาก! หนูได้เรียนไอเดียดี ๆ ของ Python เจ็ดอย่าง และรู้แล้วว่า Python ของหนูอยู่ที่ไหน แล้วเจอกันใหม่นะ!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
