Example 9 — Make a Code Name with One Loop ตัวอย่างที่ 9 — สร้างชื่อรหัสด้วย loop เดียว

The same problem as Example 8, solved with one loop that takes each letter from the end and sets its case in the same round. โจทย์เดียวกับตัวอย่างที่ 8 แต่เขียนด้วย loop เดียว ที่หยิบตัวอักษรจากท้ายและจัดตัวพิมพ์ในรอบเดียวกัน

This page solves the problem of Example 8 again, with a shorter program. Read the problem, follow the flowcharts and run the code, then see in card 4 why this program is better than the one in Example 8.

หน้านี้แก้โจทย์ของตัวอย่างที่ 8อีกครั้งด้วยโปรแกรมที่สั้นกว่า อ่านโจทย์ ไล่ตาม flowchart และรันโค้ด แล้วดูในการ์ดที่ 4 ว่าโปรแกรมนี้ดีกว่าโปรแกรมในตัวอย่างที่ 8 อย่างไร

1 The problem โจทย์

Read a name made of 1 to 10 English letters. If the name is empty, longer than 10 letters, or has anything else mixed in (spaces, digits, punctuation), warn the user and ask again. Once the name is valid, make a code name: write the name from its last letter to its first, with the first letter of the code name as a capital letter and every other letter small, then show Code name: <code name>.

รับชื่อที่เป็นตัวอักษรภาษาอังกฤษ 1 ถึง 10 ตัว ถ้าชื่อว่าง ยาวเกิน 10 ตัว หรือมีอย่างอื่นปน (ช่องว่าง ตัวเลข เครื่องหมาย) ให้แจ้งเตือนแล้วถามใหม่ เมื่อได้ชื่อที่ถูกต้องแล้ว ให้สร้างชื่อรหัส โดยเขียนชื่อจากตัวสุดท้ายไปตัวแรก ให้ตัวแรกของชื่อรหัสเป็นตัวพิมพ์ใหญ่และตัวที่เหลือเป็นตัวพิมพ์เล็กทั้งหมด แล้วแสดง Code name: <ชื่อรหัส>

  • Example: Robert → Code name: Trebor
  • is_name(text) — returns True only when text has 1 to 10 characters and every one of them is a letter (the same function as in Example 8)
  • code_name(text) — returns the code name of text in one loop
  • ตัวอย่าง: Robert → Code name: Trebor
  • is_name(text) — คืนค่า True เฉพาะเมื่อ text มี 1 ถึง 10 ตัว และทุกตัวเป็นตัวอักษร (ฟังก์ชันเดียวกับตัวอย่างที่ 8)
  • code_name(text) — คืนค่าชื่อรหัสของ text ด้วย loop เดียว
Commands used in this program:
  • isalpha() is True when the character is a letter.
  • len(text) == 0 or len(text) > 10 is True when at least one of the two checks is true.
  • A negative index counts from the end: text[-1] is the last letter, so text[-(i + 1)] is the letter at position i counted from the end, starting at 0.
  • upper() / lower() give the capital / small form of a letter.
  • while True repeats forever until break leaves the loop.
คำสั่งที่ใช้ในโปรแกรมนี้:
  • isalpha() ให้ค่า True เมื่อตัวนั้นเป็นตัวอักษร
  • len(text) == 0 or len(text) > 10 เป็น True เมื่อเงื่อนไขข้อใดข้อหนึ่งเป็นจริง
  • ดัชนีติดลบนับจากท้าย text[-1] คือตัวสุดท้าย ดังนั้น text[-(i + 1)] คือตัวอักษรตำแหน่งที่ i นับจากท้าย เริ่มที่ 0
  • upper() / lower() แปลงตัวอักษรเป็นตัวพิมพ์ใหญ่ / ตัวพิมพ์เล็ก
  • while True วนซ้ำไปเรื่อยๆ จนกว่าจะเจอ break ซึ่งสั่งให้ออกจาก loop
2 Main program — flowchart and full code โปรแกรมหลัก — flowchart และโค้ดทั้งหมด

The program starts a while True loop and reads a name into name. It checks whether the name is valid with the sub-function is_name: if it is, break leaves the loop; if not, it shows a warning with print and goes back to ask again. After leaving the loop it calls code_name(name), shows the code name with print, and the program ends.

โปรแกรมนี้เริ่มต้นด้วยวน loop while True แล้วรับชื่อเก็บใน name ตรวจสอบเงื่อนไขว่าชื่อถูกต้องหรือไม่ด้วย function ย่อย is_name ถ้าใช่ให้ break ออกจาก loop ถ้าไม่ใช่ให้แสดงข้อความเตือนด้วย print แล้วกลับไปรับค่าใหม่ หลังออกจาก loop จึงเรียก code_name(name) แสดงชื่อรหัสด้วย print และจบการทำงานของโปรแกรม

Start ask until the name is valid Keep asking while True Yes Ask for a name name = input(...) Valid name? is_name(name)? No Warn the user print('Use 1 to 10 ...') Yes Leave the loop break Show the code name print('Code name:', code_name(name)) End
The flowchart of is_name is in card 3 of Example 8; the flowchart of code_name is in card 3 below. Change the names in the [input] box or use the form under the output, then press Run. flowchart ของ is_name อยู่ในการ์ดที่ 3 ของตัวอย่างที่ 8 ส่วน flowchart ของ code_name อยู่ในการ์ดที่ 3 ด้านล่าง แก้ชื่อในกล่อง [input] หรือใช้ช่องใต้ผลลัพธ์ แล้วกด Run
[main]
[input]
[output]
Your name: Mary Ann
Use 1 to 10 letters (A-Z, a-z), please try again
Your name: Robert
Code name: Trebor

Try it yourself — the first value you submit clears the example and starts a new run. Submit one value at a time: a name that is not valid shows the warning and waits for the next value; a valid name finishes the run. Submitting after that starts a new run. Press Submit with an empty box to test an empty name.

ลองป้อนค่าเอง — ค่าแรกที่กดส่งจะล้างตัวอย่างเดิมแล้วเริ่มรันใหม่ ส่งทีละค่า: ถ้าชื่อไม่ถูกต้องจะขึ้นข้อความเตือนแล้วรอค่าถัดไป ถ้าชื่อถูกต้องโปรแกรมจะจบ และการส่งครั้งถัดไปจะเริ่มรันใหม่ ถ้ากดส่งโดยไม่พิมพ์อะไรจะเป็นการทดสอบชื่อว่าง

Results of the program (the part after Code name:) ผลของโปรแกรม (ส่วนหลัง Code name:)
Input (in order)ข้อมูลที่ป้อน (ตามลำดับ) Outputผลลัพธ์
RobertTrebor
sIMONNomis
jJ
MaximilianNailimixam
AdaAda
Christopher, Leo 1 warning, thenเตือน 1 ครั้ง แล้ว Oel
(empty), then(ว่าง) แล้ว Tom 1 warning, thenเตือน 1 ครั้ง แล้ว Mot
Mary Ann, R2D2, Kim 2 warnings, thenเตือน 2 ครั้ง แล้ว Mik
3 code_name(text)

code_name starts by setting result = '' to hold the result and i = 0 as the letter position. It checks whether i < len(text); if it is, it takes the letter text[-(i + 1)] from the end side and stores it in ch, then checks whether i == 0. If it is the first letter of the code name, it adds ch.upper() to the end of result; if not, it adds ch.lower(). Either way it increases i by 1 and checks i < len(text) again. This repeats until the condition is false, so it sends result back with return.

code_name เริ่มต้นด้วยกำหนดค่า result = '' สำหรับเก็บผลลัพธ์ และ i = 0 เป็นตัวชี้ตำแหน่งตัวอักษร แล้วตรวจสอบเงื่อนไขว่า i < len(text) หรือไม่ ถ้าใช่ให้หยิบตัวอักษร text[-(i + 1)] จากด้านท้ายเก็บใน ch แล้วตรวจสอบว่า i == 0 หรือไม่ ถ้าเป็นตัวแรกของชื่อรหัสให้ต่อ ch.upper() เข้าท้าย result ถ้าไม่ใช่ให้ต่อ ch.lower() ไม่ว่าทางใดทางหนึ่ง หลังจากนั้นเพิ่ม i ขึ้น 1 แล้วกลับไปตรวจสอบ i < len(text) อีกครั้ง ทำแบบนี้จนเงื่อนไขเป็นเท็จ จึงส่งค่า result กลับด้วย return

code_name(text) Start an empty result result = '' Start at the first letter i = 0 More letters? i < len(text)? Yes Take one letter from the end ch = text[-(i + 1)] First letter? i == 0? Yes Add it as a capital letter result += ch.upper() No Add it as a small letter result += ch.lower() Next letter i += 1 No return result
4 Why this program is better than Example 8 โปรแกรมนี้ดีกว่าตัวอย่างที่ 8 อย่างไร

Example 8 (after its bug is fixed) makes the code name in two steps: read_backward loops over the name to build backward, then name_case loops over backward again to set the case of each letter. But the case of a letter depends only on its place in the code name, which is i, and that is already known at the moment the letter is taken. So this program sets the case of each letter in the same round as it takes the letter, and never needs to keep the whole name backward.

ตัวอย่างที่ 8 (หลังแก้ bug) สร้างชื่อรหัส 2 ขั้น read_backward วน loop ตามชื่อเพื่อสร้าง backward แล้ว name_case วน loop ตาม backward อีกรอบเพื่อจัดตัวพิมพ์ทีละตัว แต่ตัวพิมพ์ของแต่ละตัวขึ้นกับตำแหน่งในชื่อรหัสเท่านั้น ซึ่งก็คือ i และรู้อยู่แล้วตั้งแต่ตอนที่หยิบตัวอักษรตัวนั้น โปรแกรมนี้จึงจัดตัวพิมพ์ในรอบเดียวกับที่หยิบตัวอักษร และไม่ต้องเก็บชื่อกลับหลังทั้งชื่อไว้

  Example 8 (fixed)ตัวอย่างที่ 8 (หลังแก้ bug) Example 9ตัวอย่างที่ 9
Functionsจำนวนฟังก์ชัน 3 (is_name, read_backward, name_case) 2 (is_name, code_name)
Loops that make the code nameจำนวน loop ที่ใช้สร้างชื่อรหัส 2 (read backward, then set the case)2 (กลับหลัง แล้วจัดตัวพิมพ์) 1
What the extra variable holdsตัวแปรเพิ่มเก็บอะไร backward holds the whole name backwardbackward เก็บชื่อกลับหลังทั้งชื่อ ch holds one letterch เก็บตัวอักษรตัวเดียว
Rounds for a 6-letter name (Robert)จำนวนรอบเมื่อชื่อยาว 6 ตัว (Robert) 12 (6 to read backward + 6 to set the case)12 (กลับหลัง 6 + จัดตัวพิมพ์ 6) 6

The index -(i + 1) also avoids the trap of Example 8: it starts at -1, the last letter, so there is no -0. Run the code below to see each round. It is the loop of code_name with one print added. Try other names too.

ดัชนี -(i + 1) ยังเลี่ยงกับดักของตัวอย่างที่ 8 ด้วย เพราะเริ่มที่ -1 ซึ่งคือตัวสุดท้าย จึงไม่มี -0 รันโค้ดด้านล่างเพื่อดูแต่ละรอบ โค้ดนี้คือ loop ของ code_name ที่เพิ่ม print 1 บรรทัด ลองป้อนชื่ออื่นด้วย

[trace]
[input]
[output]
Your name: Robert
0 -1 t T
1 -2 r Tr
2 -3 e Tre
3 -4 b Treb
4 -5 o Trebo
5 -6 R Trebor
Code name: Trebor

Think further: suppose the code name should also end with a capital letter, so that Robert gives TreboR. Which line of code_name would you change, and how?

ชวนคิดต่อ: ถ้าต้องการให้ตัวสุดท้ายของชื่อรหัสเป็นตัวพิมพ์ใหญ่ด้วย เช่น Robert ได้ TreboR ต้องแก้บรรทัดไหนของ code_name และแก้อย่างไร

Show the answer ดูคำตอบ

Change the condition if i == 0: to if i == 0 or i == len(text) - 1:. In a name of N letters, i runs from 0 to N - 1, so i == len(text) - 1 is true only in the last round, which adds the last letter of the code name. Only one loop has to change, because taking the letter and setting its case happen in the same place.

แก้เงื่อนไข if i == 0: เป็น if i == 0 or i == len(text) - 1: สำหรับชื่อยาว N ตัว i มีค่าตั้งแต่ 0 ถึง N - 1 ดังนั้น i == len(text) - 1 เป็นจริงเฉพาะรอบสุดท้าย ซึ่งเป็นรอบที่ต่อตัวสุดท้ายของชื่อรหัส แก้แค่ loop เดียวก็พอ เพราะการหยิบตัวอักษรและการจัดตัวพิมพ์อยู่ในที่เดียวกัน

[ends]
[input]
[output]
Your name: Robert
Code name: TreboR

The Run buttons on this page simulate Python with JavaScript, the same as the other lessons. To run the real Python, paste the code into Google Colab.

ปุ่ม Run ในหน้านี้จำลองโค้ด Python ด้วย JavaScript เหมือนบทเรียนอื่นๆ หากต้องการรัน Python จริง ให้คัดลอกโค้ดไปวางที่ Google Colab

References References / แหล่งอ้างอิง

  1. Python docs — String Methods (isalpha, upper, lower) (isalpha, upper, lower)
  2. Python docs — Common Sequence Operations (indexing a string, including negative indexes counted from the end) (การใช้ดัชนีกับสตริง รวมถึงดัชนีติดลบที่นับจากท้าย)
  3. Python docs — The while statement (while True and break) (while True และ break)
  4. The two-loop version of this program: Example 8 — Make a Code Name (debugging); flowchart symbols: 01 Basic → Flowchart Basic. โปรแกรมแบบ 2 loop ของโจทย์นี้: ตัวอย่างที่ 8 — สร้างชื่อรหัสจากชื่อกลับหลัง (debugging) ; สัญลักษณ์ของ flowchart: 01 Basic → Flowchart Basic

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

When i = 0, what is -(i + 1)?เมื่อ i = 0 แล้ว -(i + 1) มีค่าเท่าไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) -1-1

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 00
    Wrong: 0 is the value of -i, the trap of Example 8.ผิด: 0 คือค่าของ -i ซึ่งเป็นกับดักของตัวอย่างที่ 8
  • (B) -1-1
    Correct! -(0 + 1) = -1, so the first round takes the last letter.ถูกต้อง! -(0 + 1) = -1 จึงได้ตัวสุดท้ายตั้งแต่รอบแรก
  • (C) 11
    Wrong: the minus sign in front of the brackets makes the value negative.ผิด: เครื่องหมายลบหน้าวงเล็บทำให้ได้ค่าติดลบ
  • (D) -0, which is different from 0-0 ซึ่งต่างจาก 0
    Wrong: -(i + 1) is never 0, and -0 equals 0 anyway.ผิด: -(i + 1) ไม่เคยเป็น 0 และ -0 ก็เท่ากับ 0

ข้อ 2

2 / 10

What does code_name('Kim') return?code_name('Kim') คืนค่าอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) 'Mik''Mik'

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 'Kim''Kim'
    Wrong: code_name reads from the last letter to the first.ผิด: code_name อ่านจากตัวสุดท้ายไปตัวแรก
  • (B) 'mik''mik'
    Wrong: the round where i == 0 uses upper(), so the first letter is a capital.ผิด: รอบที่ i == 0 ใช้ upper() ตัวแรกจึงเป็นตัวพิมพ์ใหญ่
  • (C) 'MIK''MIK'
    Wrong: the second letter onwards uses lower().ผิด: ตัวที่ 2 เป็นต้นไปใช้ lower()
  • (D) 'Mik''Mik'
    Correct! Backward it is m, i, K; then the first letter becomes a capital and the rest small.ถูกต้อง! กลับหลังได้ m, i, K แล้วตัวแรกเป็นตัวพิมพ์ใหญ่ ตัวที่เหลือเป็นตัวพิมพ์เล็ก

ข้อ 3

3 / 10

In each round of code_name, what does ch hold?ในแต่ละรอบของ code_name ตัวแปร ch เก็บอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) One letter taken from the end side of the nameตัวอักษร 1 ตัวที่หยิบจากด้านท้ายของชื่อ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) One letter taken from the end side of the nameตัวอักษร 1 ตัวที่หยิบจากด้านท้ายของชื่อ
    Correct! ch = text[-(i + 1)] holds the letter at position i counted from the end.ถูกต้อง! ch = text[-(i + 1)] เก็บตัวอักษรตำแหน่งที่ i นับจากท้าย
  • (B) The whole name backwardชื่อกลับหลังทั้งชื่อ
    Wrong: this program never keeps the whole name backward; ch holds one letter at a time.ผิด: โปรแกรมนี้ไม่เก็บชื่อกลับหลังทั้งชื่อ ch เก็บทีละตัว
  • (C) The code name with its case already setชื่อรหัสที่จัดตัวพิมพ์แล้ว
    Wrong: the code name is in result.ผิด: ชื่อรหัสอยู่ใน result
  • (D) The position of the letterตำแหน่งของตัวอักษร
    Wrong: the position is i; ch holds the letter.ผิด: ตำแหน่งคือ i ส่วน ch เก็บตัวอักษร

ข้อ 4

4 / 10

How many loops does code_name use to make the code name?code_name ใช้ loop กี่ loop ในการสร้างชื่อรหัส?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 11

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 00
    Wrong: there is one loop, for i in range(len(text)).ผิด: มี for i in range(len(text)) อยู่ 1 loop
  • (B) 22
    Wrong: 2 loops is the method of Example 8 (read_backward and name_case).ผิด: 2 loop คือวิธีของตัวอย่างที่ 8 (read_backward และ name_case)
  • (C) 11
    Correct! One loop both takes each letter from the end and sets its case.ถูกต้อง! loop เดียวทั้งหยิบตัวอักษรจากท้ายและจัดตัวพิมพ์
  • (D) 33
    Wrong: code_name has only one loop.ผิด: ใน code_name มี loop เดียว

ข้อ 5

5 / 10

For the name Robert, how many rounds do the loops that make the code name run in Example 9, compared with Example 8 (fixed)?สำหรับชื่อ Robert loop ที่สร้างชื่อรหัสทำงานกี่รอบ ในตัวอย่างที่ 9 เทียบกับตัวอย่างที่ 8 (หลังแก้ bug)?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 6 rounds, compared with 126 รอบ เทียบกับ 12 รอบ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 6 rounds, compared with 126 รอบ เทียบกับ 12 รอบ
    Correct! Example 9 runs 6 rounds once; Example 8 runs 6 rounds in read_backward and 6 more in name_case.ถูกต้อง! ตัวอย่างที่ 9 วน 6 รอบครั้งเดียว ส่วนตัวอย่างที่ 8 วน 6 รอบใน read_backward และอีก 6 รอบใน name_case
  • (B) 12 rounds, compared with 612 รอบ เทียบกับ 6 รอบ
    Wrong: it is the other way round; Example 9 uses fewer rounds.ผิด: สลับกัน ตัวอย่างที่ 9 เป็นแบบที่ใช้รอบน้อยกว่า
  • (C) 6 rounds in both6 รอบเท่ากัน
    Wrong: Example 8 has 2 loops of 6 rounds each.ผิด: ตัวอย่างที่ 8 มี 2 loop แต่ละ loop วน 6 รอบ
  • (D) 3 rounds, compared with 63 รอบ เทียบกับ 6 รอบ
    Wrong: all 6 letters must be taken, so the loop needs 6 rounds.ผิด: ต้องหยิบตัวอักษรครบทั้ง 6 ตัว จึงต้องวน 6 รอบ

ข้อ 6

6 / 10

Why use text[-(i + 1)] and not text[-i]?ทำไมใช้ text[-(i + 1)] ไม่ใช้ text[-i]?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Because when i = 0, -i is 0 and gives the first letter, but -(i + 1) is -1 and gives the last oneเพราะเมื่อ i = 0 ค่า -i คือ 0 ได้ตัวแรก แต่ -(i + 1) คือ -1 ได้ตัวสุดท้าย

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Because -(i + 1) runs fasterเพราะ -(i + 1) ทำงานเร็วกว่า
    Wrong: the reason is correctness, not speed.ผิด: เหตุผลคือความถูกต้อง ไม่ใช่ความเร็ว
  • (B) Because text[-i] raises IndexErrorเพราะ text[-i] ทำให้เกิด IndexError
    Wrong: text[-i] raises no error, but it takes the wrong letter in the first round.ผิด: text[-i] ไม่เกิด error แต่ได้ตัวอักษรผิดในรอบแรก
  • (C) Because text[-i] works only for capital lettersเพราะ text[-i] ใช้ได้กับตัวพิมพ์ใหญ่เท่านั้น
    Wrong: an index has nothing to do with capital or small letters.ผิด: ดัชนีไม่เกี่ยวกับตัวพิมพ์ใหญ่หรือเล็ก
  • (D) Because when i = 0, -i is 0 and gives the first letter, but -(i + 1) is -1 and gives the last oneเพราะเมื่อ i = 0 ค่า -i คือ 0 ได้ตัวแรก แต่ -(i + 1) คือ -1 ได้ตัวสุดท้าย
    Correct! This is the bug of Example 8, which -(i + 1) avoids.ถูกต้อง! นี่คือ bug ของตัวอย่างที่ 8 ที่ -(i + 1) เลี่ยงได้

ข้อ 7

7 / 10

What does code_name('sIMON') return?code_name('sIMON') คืนค่าอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) 'Nomis''Nomis'

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 'sNOMI''sNOMI'
    Wrong: the first round takes the last letter, 'N', not the first one, and every letter gets its case set.ผิด: รอบแรกหยิบตัวสุดท้าย 'N' ไม่ใช่ตัวแรก และทุกตัวถูกจัดตัวพิมพ์
  • (B) 'Nomis''Nomis'
    Correct! Backward it is N, O, M, I, s; then the first letter becomes a capital and the rest small.ถูกต้อง! กลับหลังได้ N, O, M, I, s แล้วตัวแรกเป็นตัวพิมพ์ใหญ่ ตัวที่เหลือเป็นตัวพิมพ์เล็ก
  • (C) 'NOMIs''NOMIs'
    Wrong: this is the name backward before its case is set.ผิด: นี่คือชื่อกลับหลังที่ยังไม่ได้จัดตัวพิมพ์
  • (D) 'nomis''nomis'
    Wrong: the first letter of the code name must be a capital.ผิด: ตัวแรกของชื่อรหัสต้องเป็นตัวพิมพ์ใหญ่

ข้อ 8

8 / 10

Which name does is_name accept (return True)?ชื่อใดที่ is_name รับ (คืนค่า True)?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 'Maximilian''Maximilian'

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) ''''
    Wrong: empty text makes len(text) == 0 true, so it gives False.ผิด: ข้อความว่างทำให้ len(text) == 0 เป็นจริง จึงได้ False
  • (B) 'Christopher''Christopher'
    Wrong: it is 11 letters, more than 10.ผิด: ยาว 11 ตัว เกิน 10
  • (C) 'Maximilian''Maximilian'
    Correct! It is exactly 10 letters and every character is a letter.ถูกต้อง! ยาว 10 ตัวพอดีและเป็นตัวอักษรทั้งหมด
  • (D) 'R2D2''R2D2'
    Wrong: it has digits, so isalpha() is false.ผิด: มีตัวเลข isalpha() จึงเป็นเท็จ

ข้อ 9

9 / 10

Why does Example 9 not need the variable backward?ทำไมตัวอย่างที่ 9 ไม่ต้องมีตัวแปร backward?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) Because each letter is taken from the end and gets its case set in the same roundเพราะหยิบตัวอักษรจากท้ายและจัดตัวพิมพ์ในรอบเดียวกัน

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Because Python reverses the text by itselfเพราะ Python กลับข้อความให้เองอัตโนมัติ
    Wrong: the program still takes each letter from the end itself with text[-(i + 1)].ผิด: โปรแกรมยังหยิบตัวอักษรจากท้ายเองทีละตัวด้วย text[-(i + 1)]
  • (B) Because is_name keeps the name backwardเพราะ is_name เก็บชื่อกลับหลังไว้ให้
    Wrong: is_name only returns True or False.ผิด: is_name คืนค่าแค่ True หรือ False
  • (C) Because result keeps the name backward first and sets the case laterเพราะ result เก็บชื่อกลับหลังก่อน แล้วค่อยจัดตัวพิมพ์
    Wrong: each letter goes into result with the right case straight away.ผิด: ตัวอักษรเข้า result พร้อมตัวพิมพ์ที่ถูกต้องตั้งแต่แรก
  • (D) Because each letter is taken from the end and gets its case set in the same roundเพราะหยิบตัวอักษรจากท้ายและจัดตัวพิมพ์ในรอบเดียวกัน
    Correct! So there is no need to keep the whole name backward first.ถูกต้อง! จึงไม่ต้องเก็บชื่อกลับหลังทั้งชื่อไว้ก่อน

ข้อ 10

10 / 10

To make the last letter of the code name a capital too, what should if i == 0: become?ถ้าต้องการให้ตัวสุดท้ายของชื่อรหัสเป็นตัวพิมพ์ใหญ่ด้วย ควรเปลี่ยน if i == 0: เป็นอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) if i == 0 or i == len(text) - 1:if i == 0 or i == len(text) - 1:

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) if i == 0 or i == len(text) - 1:if i == 0 or i == len(text) - 1:
    Correct! The last round has i = len(text) - 1, so both the first and the last round use upper().ถูกต้อง! รอบสุดท้ายมี i = len(text) - 1 จึงใช้ upper() ทั้งรอบแรกและรอบสุดท้าย
  • (B) if i == 0 or i == len(text):if i == 0 or i == len(text):
    Wrong: i goes up to len(text) - 1 only, so the check after or is never true and the last letter stays small.ผิด: i มีค่าสูงสุดแค่ len(text) - 1 เงื่อนไขหลัง or จึงไม่เคยเป็นจริง ตัวสุดท้ายยังเป็นตัวพิมพ์เล็ก
  • (C) if i == len(text):if i == len(text):
    Wrong: i goes up to len(text) - 1 only, so this check is never true.ผิด: i มีค่าสูงสุดแค่ len(text) - 1 เงื่อนไขนี้จึงไม่เคยเป็นจริง
  • (D) if i == -1:if i == -1:
    Wrong: i starts at 0 and goes up, so it is never -1.ผิด: i เริ่มที่ 0 และเพิ่มขึ้น จึงไม่เคยเป็น -1