@@ -59,6 +59,19 @@ msgid ""
5959"...\n"
6060"More"
6161msgstr ""
62+ ">>> x = int(input(\" Please enter an integer:\" ))\n"
63+ "Please enter an integer: 42\n"
64+ ">>> if x < 0:\n"
65+ "... x = 0\n"
66+ "... print('Negative changed to zero')\n"
67+ "... elif x == 0:\n"
68+ "... print('Zero')\n"
69+ "... elif x == 1:\n"
70+ "... print('Single')\n"
71+ "... else:\n"
72+ "... print('More')\n"
73+ "...\n"
74+ "More"
6275
6376#: ../../tutorial/controlflow.rst:33
6477msgid ""
@@ -112,6 +125,14 @@ msgid ""
112125"window 6\n"
113126"defenestrate 12"
114127msgstr ""
128+ ">>> # 測量一些字串:\n"
129+ ">>> words = ['cat', 'window', 'defenestrate']\n"
130+ ">>> for w in words:\n"
131+ "... print(w, len(w))\n"
132+ "...\n"
133+ "cat 3\n"
134+ "window 6\n"
135+ "defenestrate 12"
115136
116137#: ../../tutorial/controlflow.rst:72
117138msgid ""
@@ -138,6 +159,19 @@ msgid ""
138159" if status == 'active':\n"
139160" active_users[user] = status"
140161msgstr ""
162+ "# 建立一個範例集合\n"
163+ "users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}\n"
164+ "\n"
165+ "# 策略:對副本進行疊代\n"
166+ "for user, status in users.copy().items():\n"
167+ " if status == 'inactive':\n"
168+ " del users[user]\n"
169+ "\n"
170+ "# 策略:建立一個新集合\n"
171+ "active_users = {}\n"
172+ "for user, status in users.items():\n"
173+ " if status == 'active':\n"
174+ " active_users[user] = status"
141175
142176#: ../../tutorial/controlflow.rst:94
143177msgid "The :func:`range` Function"
@@ -376,6 +410,9 @@ msgid ""
376410"finishes without executing the :keyword:`!break`, the :keyword:`!else` "
377411"clause executes."
378412msgstr ""
413+ "在 :keyword:`!for` 或 :keyword:`!while` 迴圈中,:keyword:`!break` "
414+ "陳述句可能與 :keyword:`!else` 子句配對。如果迴圈完成而沒有執行 "
415+ ":keyword:`!break`,:keyword:`!else` 子句會被執行。"
379416
380417#: ../../tutorial/controlflow.rst:208
381418msgid ""
@@ -427,6 +464,23 @@ msgid ""
427464"8 equals 2 * 4\n"
428465"9 equals 3 * 3"
429466msgstr ""
467+ ">>> for n in range(2, 10):\n"
468+ "... for x in range(2, n):\n"
469+ "... if n % x == 0:\n"
470+ "... print(n, 'equals', x, '*', n//x)\n"
471+ "... break\n"
472+ "... else:\n"
473+ "... # 迴圈結束但沒有找到因數\n"
474+ "... print(n, 'is a prime number')\n"
475+ "...\n"
476+ "2 is a prime number\n"
477+ "3 is a prime number\n"
478+ "4 equals 2 * 2\n"
479+ "5 is a prime number\n"
480+ "6 equals 2 * 3\n"
481+ "7 is a prime number\n"
482+ "8 equals 2 * 4\n"
483+ "9 equals 3 * 3"
430484
431485#: ../../tutorial/controlflow.rst:239
432486msgid ""
@@ -444,6 +498,9 @@ msgid ""
444498"condition is ever true, a ``break`` will happen. If the condition is never "
445499"true, the ``else`` clause outside the loop will execute."
446500msgstr ""
501+ "理解 else 子句的一個方法是將它想像成與迴圈內的 ``if`` 配對。當迴圈執行時,它會"
502+ "運行如 if/if/if/else 的序列。``if`` 在迴圈內,會遇到多次。如果條件曾經為真,"
503+ "``break`` 就會發生。如果條件從未為真,迴圈外的 ``else`` 子句就會執行。"
447504
448505#: ../../tutorial/controlflow.rst:248
449506msgid ""
@@ -478,6 +535,9 @@ msgid ""
478535"... pass # Busy-wait for keyboard interrupt (Ctrl+C)\n"
479536"..."
480537msgstr ""
538+ ">>> while True:\n"
539+ "... pass # 忙碌等待鍵盤中斷 (Ctrl+C)\n"
540+ "..."
481541
482542#: ../../tutorial/controlflow.rst:266
483543msgid "This is commonly used for creating minimal classes::"
@@ -509,6 +569,9 @@ msgid ""
509569"... pass # Remember to implement this!\n"
510570"..."
511571msgstr ""
572+ ">>> def initlog(*args):\n"
573+ "... pass # 記得要實作這個!\n"
574+ "..."
512575
513576#: ../../tutorial/controlflow.rst:284
514577msgid ":keyword:`!match` Statements"
@@ -604,6 +667,18 @@ msgid ""
604667" case _:\n"
605668" raise ValueError(\" Not a point\" )"
606669msgstr ""
670+ "# point 是一個 (x, y) 元組\n"
671+ "match point:\n"
672+ " case (0, 0):\n"
673+ " print(\" Origin\" )\n"
674+ " case (0, y):\n"
675+ " print(f\" Y={y}\" )\n"
676+ " case (x, 0):\n"
677+ " print(f\" X={x}\" )\n"
678+ " case (x, y):\n"
679+ " print(f\" X={x}, Y={y}\" )\n"
680+ " case _:\n"
681+ " raise ValueError(\" Not a point\" )"
607682
608683#: ../../tutorial/controlflow.rst:331
609684msgid ""
@@ -912,6 +987,17 @@ msgid ""
912987">>> fib(2000)\n"
913988"0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597"
914989msgstr ""
990+ ">>> def fib(n): # 寫出小於 n 的費氏數列\n"
991+ "...\"\"\" 印出小於 n 的費氏數列。\"\"\" \n"
992+ "... a, b = 0, 1\n"
993+ "... while a < n:\n"
994+ "... print(a, end=' ')\n"
995+ "... a, b = b, a+b\n"
996+ "... print()\n"
997+ "...\n"
998+ ">>> # 現在呼叫我們剛才定義的函式:\n"
999+ ">>> fib(2000)\n"
1000+ "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597"
9151001
9161002#: ../../tutorial/controlflow.rst:482
9171003msgid ""
@@ -1045,6 +1131,18 @@ msgid ""
10451131">>> f100 # write the result\n"
10461132"[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
10471133msgstr ""
1134+ ">>> def fib2(n): # 回傳到 n 為止的費氏數列\n"
1135+ "...\"\"\" 回傳包含到 n 為止的費氏數列的串列。\"\"\" \n"
1136+ "... result = []\n"
1137+ "... a, b = 0, 1\n"
1138+ "... while a < n:\n"
1139+ "... result.append(a) # 見下方\n"
1140+ "... a, b = b, a+b\n"
1141+ "... return result\n"
1142+ "...\n"
1143+ ">>> f100 = fib2(100) # 呼叫它\n"
1144+ ">>> f100 # 寫出結果\n"
1145+ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]"
10481146
10491147#: ../../tutorial/controlflow.rst:550
10501148msgid "This example, as usual, demonstrates some new Python features:"
@@ -1301,6 +1399,12 @@ msgid ""
13011399"parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 "
13021400"keyword"
13031401msgstr ""
1402+ "parrot(1000) # 1 個位置引數\n"
1403+ "parrot(voltage=1000) # 1 個關鍵字引數\n"
1404+ "parrot(voltage=1000000, action='VOOOOOM') # 2 個關鍵字引數\n"
1405+ "parrot(action='VOOOOOM', voltage=1000000) # 2 個關鍵字引數\n"
1406+ "parrot('a million', 'bereft of life', 'jump') # 3 個位置引數\n"
1407+ "parrot('a thousand', state='pushing up the daisies') # 1 個位置引數、1 個關鍵字引數\n"
13041408
13051409#: ../../tutorial/controlflow.rst:677
13061410msgid "but all the following calls would be invalid::"
@@ -1314,6 +1418,10 @@ msgid ""
13141418"parrot(110, voltage=220) # duplicate value for the same argument\n"
13151419"parrot(actor='John Cleese') # unknown keyword argument"
13161420msgstr ""
1421+ "parrot() # 缺少必要引數\n"
1422+ "parrot(voltage=5.0, 'dead') # 關鍵字引數後面的非關鍵字引數\n"
1423+ "parrot(110, voltage=220) # 同一個引數有重複值\n"
1424+ "parrot(actor='John Cleese') # 未知的關鍵字引數"
13171425
13181426#: ../../tutorial/controlflow.rst:684
13191427msgid ""
@@ -1877,6 +1985,11 @@ msgid ""
18771985"list\n"
18781986"[3, 4, 5]"
18791987msgstr ""
1988+ ">>> list(range(3, 6)) # 使用分離引數的一般呼叫\n"
1989+ "[3, 4, 5]\n"
1990+ ">>> args = [3, 6]\n"
1991+ ">>> list(range(*args)) # 以從串列解包而得的引數呼叫\n"
1992+ "[3, 4, 5]"
18801993
18811994#: ../../tutorial/controlflow.rst:965
18821995msgid ""
@@ -2041,6 +2154,17 @@ msgid ""
20412154"\n"
20422155"No, really, it doesn't do anything."
20432156msgstr ""
2157+ ">>> def my_function():\n"
2158+ "...\"\"\" 不做任何事,但有文件說明。\n"
2159+ "...\n"
2160+ "... 不,真的,它什麼都不做。\n"
2161+ "...\"\"\" \n"
2162+ "... pass\n"
2163+ "...\n"
2164+ ">>> print(my_function.__doc__)\n"
2165+ "Do nothing, but document it.\n"
2166+ "\n"
2167+ "No, really, it doesn't do anything."
20442168
20452169#: ../../tutorial/controlflow.rst:1064
20462170msgid "Function Annotations"