@@ -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+ ">>> # Measure some strings:\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+ "# Create a sample collection\n"
163+ "users = {'Hans': 'active', 'Éléonore': 'inactive', '景太郎': 'active'}\n"
164+ "\n"
165+ "# Strategy: Iterate over a copy\n"
166+ "for user, status in users.copy().items():\n"
167+ " if status == 'inactive':\n"
168+ " del users[user]\n"
169+ "\n"
170+ "# Strategy: Create a new collection\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+ "... # loop fell through without finding a factor\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 # Busy-wait for keyboard interrupt (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 # Remember to implement this!\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 is an (x, y) tuple\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): # write Fibonacci series less than n\n"
991+ "...\"\"\" Print a Fibonacci series less than 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+ ">>> # Now call the function we just defined:\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): # return Fibonacci series up to n\n"
1135+ "...\"\"\" Return a list containing the Fibonacci series up to n.\"\"\" \n"
1136+ "... result = []\n"
1137+ "... a, b = 0, 1\n"
1138+ "... while a < n:\n"
1139+ "... result.append(a) # see below\n"
1140+ "... a, b = b, a+b\n"
1141+ "... return result\n"
1142+ "...\n"
1143+ ">>> f100 = fib2(100) # call it\n"
1144+ ">>> f100 # write the result\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,15 @@ msgid ""
13011399"parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 "
13021400"keyword"
13031401msgstr ""
1402+ "parrot(1000) # 1 positional "
1403+ "argument\n"
1404+ "parrot(voltage=1000) # 1 keyword argument\n"
1405+ "parrot(voltage=1000000, action='VOOOOOM') # 2 keyword arguments\n"
1406+ "parrot(action='VOOOOOM', voltage=1000000) # 2 keyword arguments\n"
1407+ "parrot('a million', 'bereft of life', 'jump') # 3 positional "
1408+ "arguments\n"
1409+ "parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 "
1410+ "keyword"
13041411
13051412#: ../../tutorial/controlflow.rst:677
13061413msgid "but all the following calls would be invalid::"
@@ -1314,6 +1421,11 @@ msgid ""
13141421"parrot(110, voltage=220) # duplicate value for the same argument\n"
13151422"parrot(actor='John Cleese') # unknown keyword argument"
13161423msgstr ""
1424+ "parrot() # required argument missing\n"
1425+ "parrot(voltage=5.0, 'dead') # non-keyword argument after a keyword "
1426+ "argument\n"
1427+ "parrot(110, voltage=220) # duplicate value for the same argument\n"
1428+ "parrot(actor='John Cleese') # unknown keyword argument"
13171429
13181430#: ../../tutorial/controlflow.rst:684
13191431msgid ""
@@ -1877,6 +1989,12 @@ msgid ""
18771989"list\n"
18781990"[3, 4, 5]"
18791991msgstr ""
1992+ ">>> list(range(3, 6)) # normal call with separate arguments\n"
1993+ "[3, 4, 5]\n"
1994+ ">>> args = [3, 6]\n"
1995+ ">>> list(range(*args)) # call with arguments unpacked from a "
1996+ "list\n"
1997+ "[3, 4, 5]"
18801998
18811999#: ../../tutorial/controlflow.rst:965
18822000msgid ""
@@ -2041,6 +2159,17 @@ msgid ""
20412159"\n"
20422160"No, really, it doesn't do anything."
20432161msgstr ""
2162+ ">>> def my_function():\n"
2163+ "...\"\"\" Do nothing, but document it.\n"
2164+ "...\n"
2165+ "... No, really, it doesn't do anything.\n"
2166+ "...\"\"\" \n"
2167+ "... pass\n"
2168+ "...\n"
2169+ ">>> print(my_function.__doc__)\n"
2170+ "Do nothing, but document it.\n"
2171+ "\n"
2172+ "No, really, it doesn't do anything."
20442173
20452174#: ../../tutorial/controlflow.rst:1064
20462175msgid "Function Annotations"