Testing and Debugging การทดสอบและการแก้ไขโปรแกรม (Testing and Debugging)

A step-by-step way to hunt bugs: know the kinds of error, choose good test data, then fix two example programs that each hide one bug. ฝึกหา bug อย่างเป็นขั้นตอน ตั้งแต่รู้จักชนิดของ error และเลือกข้อมูลทดสอบ ไปจนถึงลงมือแก้โปรแกรมตัวอย่าง 2 ข้อที่มี bug ซ่อนอยู่

1 What is a bug? 1 Bug คืออะไร

Programs are almost never fully correct the first time they are written. A mistake that makes a program behave differently from what we intended is called a bug. Some bugs stop the program from running at all, some make it stop halfway, and some show no sign at all: the program runs to the end but gives the wrong answer.

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

Working with bugs has two parts:

งานที่เกี่ยวกับ bug แบ่งเป็น 2 ส่วน

  • Testing — running the program with data whose answer we already know. If the result does not match that answer, there is a bug.
  • Debugging — tracking down which line the bug is on and what causes it, then fixing it.
  • Testing คือการรันโปรแกรมด้วยข้อมูลที่เรารู้คำตอบอยู่แล้ว ถ้าผลที่ได้ไม่ตรงกับคำตอบ แสดงว่ามี bug
  • Debugging คือการตามหาว่า bug อยู่บรรทัดไหน เกิดจากอะไร แล้วแก้ให้ถูก

Testing only tells us that something is wrong; debugging finds out where it is wrong.

testing บอกเราแค่ว่า "มีปัญหา" ส่วน debugging ตอบว่า "ปัญหาอยู่ตรงไหน"

A bit of history: engineers called machine faults "bugs" long before computers existed, but the story most often told comes from 1947. The team running the Harvard Mark II computer found a moth stuck in a relay, taped it into their operations log book, and noted it as the first actual case of a bug being found.

เกร็ดความรู้: คำว่า bug ใช้เรียกข้อบกพร่องของเครื่องจักรมาตั้งแต่ก่อนยุคคอมพิวเตอร์ แต่เรื่องที่มักถูกยกมาเล่าเกิดขึ้นในปี 1947 ทีมดูแลเครื่อง Harvard Mark II พบผีเสื้อกลางคืนติดอยู่ใน relay จึงแปะซากผีเสื้อลงในสมุดบันทึกการทำงาน พร้อมเขียนว่าเป็นกรณีแรกที่พบ bug ตัวจริง

Bug-hunting tools used in this lesson เครื่องมือหา bug ที่ใช้ในบทนี้

  1. print() along the way — print the values of variables at the spot you suspect.
  2. Comment out part of the code with # — switch some lines off for a moment and test the program one part at a time, to see where the values first go wrong.
  3. Trace table — a table where you follow the value of each variable round by round, by hand. It works well for loops.
  1. print() ระหว่างทาง — พิมพ์ค่าตัวแปรออกมาดูในจุดที่สงสัย
  2. ปิดโค้ดบางส่วนด้วย # — ทำให้บางบรรทัดไม่ทำงานชั่วคราว แล้วทดสอบทีละช่วง เพื่อหาว่าช่วงไหนเริ่มให้ค่าผิด
  3. trace table — ตารางที่ไล่ค่าตัวแปรทีละรอบด้วยมือ เหมาะกับ loop

2 Types of program errors 2 ชนิดของ error ในโปรแกรม

Kindชนิด When Python finds itPython พบเมื่อไร Does the program run?โปรแกรมทำงานไหม Error message?มีข้อความ error ไหม
Syntax error Before the run startsก่อนเริ่มรัน Not a single line runsไม่ทำงานเลยสักบรรทัด Yesมี
Run-time error During the runระหว่างรัน Runs up to the failing line, then stopsทำงานไปจนถึงบรรทัดที่ผิด แล้วหยุด Yesมี
Logic error Neverไม่พบ Runs to the endทำงานจนจบ No — but the answer is wrongไม่มี แต่คำตอบผิด

2.1 Syntax error 2.1 Syntax error

The code breaks a grammar rule of Python, so Python cannot read it even before the run starts. Common causes are a missing : at the end of if / for / def, a bracket that is never closed, or lines that are not indented consistently. Here line 3 has no :, so even print("Start") on line 1 never runs. Add the : and press Run again.

เขียนผิดกฎไวยากรณ์ของภาษา Python จึงอ่านโค้ดไม่ออกตั้งแต่ก่อนรัน ตัวอย่างที่พบบ่อยคือลืม : ท้าย if / for / def ลืมปิดวงเล็บ หรือย่อหน้าไม่ตรงกัน ในตัวอย่างนี้บรรทัดที่ 3 ไม่มี : จึงไม่มีบรรทัดไหนได้ทำงาน แม้แต่ print("Start") ในบรรทัดที่ 1 ลองเติม : แล้วกด Run อีกครั้ง

[2.1]
[output]
SyntaxError: expected ':' (line 3)

2.2 Run-time error 2.2 Run-time error

The grammar is correct, but during the run the program reaches an instruction it cannot carry out, so it stops at that line. The lines before it have already run. Below, scores has only 3 items (index 0–2), so scores[3] does not exist.

โค้ดถูกไวยากรณ์ แต่ระหว่างรันไปเจอคำสั่งที่ทำไม่ได้ โปรแกรมจึงหยุดที่บรรทัดนั้น และบรรทัดก่อนหน้าได้ทำงานไปแล้ว ในตัวอย่างนี้ scores มีแค่ 3 ตัว (ดัชนี 0–2) จึงไม่มี scores[3]

[2.2a]
[output]
First: 80
IndexError: list index out of range

Another common run-time error: finding the average of an empty list divides by len(scores), which is 0.

run-time error ที่พบบ่อยอีกแบบ: หาค่าเฉลี่ยของลิสต์ว่าง ทำให้ต้องหารด้วย len(scores) ซึ่งเท่ากับ 0

[2.2b]
[output]
Total: 0
ZeroDivisionError: division by zero

2.3 Logic error 2.3 Logic error

The program runs to the end with no error message, but the answer is wrong because the steps we wrote are not the steps we meant. This is the hardest kind of bug to find, because Python gives no warning at all: you only see it by comparing the result with an answer you already know. The average of 60 and 80 is 70, yet the program prints 100.0, because Python divides before it adds. Change the line to (a + b) / 2 and you get 70.0.

โปรแกรมรันจนจบ ไม่มีข้อความ error แต่คำตอบผิด เพราะขั้นตอนที่เขียนไม่ตรงกับที่ตั้งใจ เป็น bug ที่หายากที่สุด เพราะ Python ไม่เตือนอะไรเลย ต้องเทียบผลกับคำตอบที่รู้อยู่แล้วจึงจะเห็น ค่าเฉลี่ยของ 60 กับ 80 คือ 70 แต่โปรแกรมพิมพ์ 100.0 ที่ผิดเพราะ Python หารก่อนบวก แก้เป็น (a + b) / 2 จะได้ 70.0

[2.3]
[output]
100.0

3 Types of test cases 3 ชนิดของ test case

Test data picked at random often misses the bugs that are hiding, so choose test data from all three groups below. The example is a function check_score(text). It receives a score as text; a valid score is 0–100. It returns "Pass" for 50 or more, "Fail" for less than 50, and "Invalid" for anything that is not a valid score.

เลือกข้อมูลทดสอบแบบสุ่มๆ มักไม่เจอ bug ที่ซ่อนอยู่ จึงควรเลือกให้ครบ 3 กลุ่ม โดยใช้ฟังก์ชัน check_score(text) เป็นตัวอย่าง ฟังก์ชันนี้รับคะแนนเป็นข้อความ คะแนนที่ถูกต้องอยู่ในช่วง 0–100 ถ้าได้ 50 ขึ้นไปคืนค่า "Pass" ถ้าต่ำกว่า 50 คืนค่า "Fail" และถ้าไม่ใช่คะแนนที่ถูกต้องคืนค่า "Invalid"

  1. Normal test case — ordinary data that users enter every day, well inside the accepted range. It checks that the main job of the program is right, e.g. 72 → Pass, 35 → Fail.
  2. Boundary test case — data right on the edge of the accepted range, and the value just past the edge. Bugs such as writing > instead of >= like to hide here, e.g. 0, 49, 50, 100, 101. Empty text and text with a single character also count as edge data.
  3. Error test case — data of the wrong kind or in the wrong form. A good program must reject it without stopping halfway, e.g. abc, -5, 7.5.
  1. Normal test case — ข้อมูลทั่วไปที่ผู้ใช้ป้อนกันเป็นปกติ อยู่กลางช่วงที่รับได้ ใช้ตรวจว่างานหลักของโปรแกรมถูก เช่น 72 → Pass, 35 → Fail
  2. Boundary test case — ข้อมูลที่อยู่ตรงขอบของช่วงที่รับได้ และค่าที่เลยขอบไปเพียงหนึ่งค่า bug ประเภทใช้ > แทน >= มักซ่อนอยู่ตรงนี้ เช่น 0, 49, 50, 100, 101 ข้อความว่างหรือข้อความที่มีตัวอักษรตัวเดียวก็นับเป็นข้อมูลที่ขอบเช่นกัน
  3. Error test case — ข้อมูลที่ผิดชนิดหรือผิดรูปแบบ โปรแกรมที่ดีต้องปฏิเสธข้อมูลนั้นโดยไม่หยุดทำงานกลางทาง เช่น abc, -5, 7.5

Note: some textbooks put a value just past the edge (101, -1) in the error group instead, so the grouping can differ. What matters is that these values get tested.

หมายเหตุ: ค่าที่เลยขอบไปหนึ่งค่า (101, -1) บางตำราจัดเป็น error test case การจัดกลุ่มจึงต่างกันได้ สิ่งสำคัญคือต้องทดสอบค่าเหล่านี้

3.1 Running a test plan 3.1 รันตามแผนการทดสอบ

isdigit() is True only when every character of the text is a digit, so "-5" and "7.5" are rejected before int() converts the text to a number. The [input] box feeds the nine test values to input() one line at a time.

isdigit() ให้ค่า True เมื่อทุกตัวอักษรในข้อความเป็นตัวเลข "-5" และ "7.5" จึงถูกปฏิเสธก่อนที่ int() จะแปลงข้อความเป็นตัวเลข กล่อง [input] ป้อนค่าทดสอบทั้ง 9 ค่าให้ input() ทีละบรรทัด

[3.1]
[input]
[output]
Score: 72
  -> Pass
Score: 35
  -> Fail
Score: 0
  -> Fail
Score: 49
  -> Fail
Score: 50
  -> Pass
Score: 100
  -> Pass
Score: 101
  -> Invalid
Score: abc
  -> Invalid
Score: -5
  -> Invalid

Write the plan as a table before you run it: the expected output comes from the problem, the actual output from the run. Section 4 uses the same table.

เขียนแผนเป็นตารางก่อนรัน: ผลที่ควรได้มาจากโจทย์ ส่วนผลที่ได้จริงมาจากการรัน ส่วนที่ 4 ใช้ตารางแบบเดียวกันนี้

Inputข้อมูลที่ป้อน Kindชนิด Expected outputผลที่ควรได้ Actual outputผลที่ได้จริง Pass?ผ่าน?
72NormalPassPass✓
35NormalFailFail✓
0BoundaryFailFail✓
49BoundaryFailFail✓
50BoundaryPassPass✓
100BoundaryPassPass✓
101BoundaryInvalidInvalid✓
abcErrorInvalidInvalid✓
-5ErrorInvalidInvalid✓

4 Find and fix the bug 4 ลองหาและแก้ bug

Each of the two programs below hides one bug. Read the problem and the flowchart, then check the code against the flowchart box by box. Run the test list, find the wrong line, and fix the program in the code box yourself. After fixing it, run the test list again until every test gives its expected output. Only then open the solution.

โปรแกรม 2 ข้อด้านล่างมี bug ซ่อนอยู่ข้อละ 1 จุด ให้อ่านโจทย์และ flowchart แล้วตรวจโค้ดเทียบกับ flowchart ทีละกล่อง จากนั้นรันตามรายการทดสอบ หาบรรทัดที่ผิด และแก้โปรแกรมในกล่องโค้ดให้ถูกต้องเอง เมื่อแก้แล้วให้รันรายการทดสอบซ้ำ จนทุกข้อได้ผลตามที่ควรได้ หลังจากนั้นจึงเปิดดูเฉลย

Example 1 — Flip the letters of every word (flip_words) ตัวอย่างที่ 1 — กลับตัวอักษรทุกคำ (flip_words)

1 The problem โจทย์

Read a sentence that contains only English letters and spaces, then show the same sentence with the order of the letters reversed inside every word. The words stay in their original order, and they are always separated by exactly one space, even if the sentence typed in has extra spaces.

รับประโยคที่มีเฉพาะตัวอักษรภาษาอังกฤษและช่องว่าง แล้วแสดงประโยคเดิมโดยกลับลำดับตัวอักษรในแต่ละคำ ลำดับของคำยังเหมือนเดิม และคั่นแต่ละคำด้วยช่องว่าง 1 ช่องเสมอ แม้ประโยคที่ป้อนจะมีช่องว่างเกินมา

  • Example:ตัวอย่าง: Python is fun → nohtyP si nuf

New in this program: split() cuts text into a list of words, using spaces as the separator (several spaces in a row count as one separator). A negative index counts from the end: [-1] is the last character and [-2] the one before it. [:-1] drops the last character.

คำสั่งใหม่ในโปรแกรมนี้: split() แยกข้อความเป็นลิสต์ของคำ โดยใช้ช่องว่างเป็นตัวคั่น (ช่องว่างหลายช่องติดกันนับเป็นตัวคั่นเดียว) ดัชนีติดลบนับจากท้าย [-1] คือตัวสุดท้าย และ [-2] คือตัวรองสุดท้าย ส่วน [:-1] ตัดตัวสุดท้ายทิ้ง

2 Flowchart and code (contains a bug) flowchart และโค้ด (มี bug)

The program starts by reading a sentence into sentence and cutting it into a list of words with split, stored in words. Next it sets i = 0 as the position of the current word and checks whether i < len(words). If it is, it creates flipped = "" to collect the reversed word and pos = 0 as the letter position, then checks whether pos < len(words[i]). If it is, it adds the letter words[i][-pos] to the end of flipped, increases pos by 1 and checks again. This repeats until the condition is false; then it stores flipped back in words[i], increases i by 1 and goes back to check i < len(words) again. When every word is done it leaves the loop, sets output_line = "" and i = 0, and loops once more to add each word followed by one space to output_line. Finally it removes the last space with output_line[:-1], shows the result with print, and the program ends.

โปรแกรมนี้เริ่มต้นด้วยรับข้อความจากผู้ใช้เก็บใน sentence แล้วแยกเป็นลิสต์ของคำด้วย split เก็บใน words ถัดไปกำหนดค่า i = 0 เป็นตัวชี้ลำดับคำ แล้วตรวจสอบเงื่อนไขว่า i < len(words) หรือไม่ ถ้าใช่ให้สร้างตัวแปร flipped = "" สำหรับเก็บคำที่กลับแล้ว และ pos = 0 เป็นตัวชี้ตำแหน่งตัวอักษร จากนั้นตรวจสอบว่า pos < len(words[i]) หรือไม่ ถ้าใช่ให้ต่อตัวอักษร words[i][-pos] เข้าท้าย flipped แล้วเพิ่ม pos ขึ้น 1 และกลับไปตรวจสอบอีกครั้ง ทำแบบนี้จนเงื่อนไขเป็นเท็จ จึงเก็บ flipped กลับลงใน words[i] เพิ่ม i ขึ้น 1 แล้วกลับไปตรวจสอบ i < len(words) อีกครั้ง เมื่อครบทุกคำจึงออกจาก loop หลังจากนั้นกำหนด output_line = "" และ i = 0 แล้ววน loop ต่อแต่ละคำตามด้วยช่องว่างเข้าไปใน output_line เมื่อครบแล้วตัดช่องว่างตัวสุดท้ายออกด้วย output_line[:-1] แล้วแสดงผลด้วย print และจบการทำงานของโปรแกรม

Start Read a sentence sentence = input(...) Cut the sentence into words words = sentence.split() Start at the first word i = 0 flip the letters of each word More words to flip? i < len(words)? No Yes Start an empty flipped word flipped = "" Start at the first letter pos = 0 More letters in this word? pos < len(words[i])? Yes Add one letter to flipped flipped += words[i][-pos] Move to the next letter pos += 1 No Store the flipped word words[i] = flipped Move to the next word i += 1 join the words with one space Start an empty output line output_line = "" Back to the first word i = 0 More words to join? i < len(words)? Yes Add the word and one space output_line += words[i] + " " Move to the next word i += 1 No Remove the last space output_line = output_line[:-1] Show the result print(output_line) End
The code below follows this flowchart line for line, including its bug. Change the sentence in the [input] box or use the form under the output, then press Run. โค้ดด้านล่างเขียนตาม flowchart นี้ทุกกล่อง รวมทั้ง bug ด้วย แก้ประโยคในกล่อง [input] หรือใช้ช่องใต้ผลลัพธ์ แล้วกด Run
[main]
[input]
[output]
Type a sentence: Python is fun
Pnohty is fnu

Try it yourself — type a sentence and press Submit. It replaces the [input] box and runs the code that is in the box right now (including your fixes). Submit an empty box to test empty input.

ลองป้อนค่าเอง — พิมพ์ประโยคแล้วกดส่ง ประโยคจะแทนที่กล่อง [input] แล้วรันโค้ดที่อยู่ในกล่องตอนนี้ (รวมถึงส่วนที่คุณแก้แล้ว) ถ้ากดส่งโดยไม่พิมพ์อะไรจะเป็นการทดสอบข้อความว่าง

3 Test list รายการทดสอบ

Run each input yourself (type it into the form above) and check the result against the expected output column. The last column shows what the code with the bug gives.

รันทีละข้อด้วยตัวเอง (พิมพ์ลงในช่องด้านบน) แล้วเทียบผลกับคอลัมน์ผลที่ควรได้ คอลัมน์สุดท้ายคือผลจากโค้ดที่ยังมี bug

Inputข้อมูลที่ป้อน Kindชนิด Expected outputผลที่ควรได้ Output of the buggy codeผลจากโค้ดที่มี bug
Python is fun Normal nohtyP si nuf Pnohty is fnu ✗
Debug every loop Normal gubeD yreve pool Dgube eyrev lpoo ✗
a I Boundary (one-letter words)Boundary (คำละตัวอักษรเดียว) a I a I ✓
(empty)(ว่าง) Boundary (no words)Boundary (ไม่มีคำเลย) an empty lineบรรทัดว่าง an empty lineบรรทัดว่าง ✓
  Hi   there   Boundary (extra spaces)Boundary (ช่องว่างเกิน) iH ereht Hi tereh ✗
Hi 2 you! Error (digit and punctuation)Error (มีตัวเลขและเครื่องหมาย) should be rejected, but this program does not check its input; after the fix it gives iH 2 !uoy ควรถูกปฏิเสธ แต่โปรแกรมนี้ไม่ได้ตรวจข้อมูลที่รับเข้ามา หลังแก้แล้วจะได้ iH 2 !uoy Hi 2 y!uo
4 Your tasks ภารกิจ
  1. Check the code against the flowchart box by box, and note anything that looks suspicious.
  2. Run every input in the test list and note whether the actual output matches the expected output.
  3. a I gives the right answer even though the program has a bug. Why? And what pattern does the wrong output of Python is fun show — which letter ends up in the wrong place?
  4. Which kind of error is this bug, and why?
  5. Find the wrong line:
    • put # in front of the join part and add print(words) after the first loop, to see whether the values are already wrong in the flip part, or
    • add print(pos, -pos, words[i][-pos]) inside the inner loop,
    • then fill in the trace table below for the word fun.
  6. Fix the code in the box and run the test list again. Every Normal and Boundary input must give its expected output.
  7. Think further: Hi 2 you! breaks the rule of the problem, yet the program does not reject it. Where would you add a check? (Example 2 shows one way.)
  1. ตรวจโค้ดเทียบกับ flowchart ทีละกล่อง แล้วจดจุดที่สงสัย
  2. รันทุกข้อในรายการทดสอบ แล้วจดผลที่ได้จริงว่าตรงกับผลที่ควรได้หรือไม่
  3. a I ได้ผลถูกทั้งที่โปรแกรมมี bug เพราะอะไร และผลที่ผิดของ Python is fun มีรูปแบบอย่างไร (ตัวอักษรตัวไหนอยู่ผิดที่)
  4. bug นี้เป็น error ชนิดไหน เพราะอะไร
  5. หาบรรทัดที่ผิด
    • ใส่ # ปิดช่วงต่อคำ แล้วใส่ print(words) หลัง loop แรก เพื่อดูว่าค่าผิดตั้งแต่ช่วงกลับตัวอักษรหรือไม่ หรือ
    • ใส่ print(pos, -pos, words[i][-pos]) ใน loop ใน
    • แล้วกรอก trace table ด้านล่างของคำว่า fun
  6. แก้โค้ดในกล่อง แล้วรันรายการทดสอบซ้ำ ข้อที่เป็น Normal และ Boundary ต้องได้ผลตามตารางทุกข้อ
  7. ชวนคิดต่อ: Hi 2 you! ผิดเงื่อนไขของโจทย์ แต่โปรแกรมไม่ปฏิเสธ ควรเพิ่มการตรวจตรงไหน (ตัวอย่างที่ 2 มีวิธีหนึ่งให้ดู)
Trace table: words[i] = "fun" trace table: words[i] = "fun"
pos-poswords[i][-pos]flipped
0   
1   
2   
5 Solution เฉลย
Show the fixed code ดูโค้ดที่แก้แล้ว

The bug is on the line flipped += words[i][-pos]. Because pos starts at 0, -pos is -0, which is the same as 0, so the first round takes the first letter instead of the last one. Change it to flipped += words[i][-(pos + 1)]. It is a logic error: the program runs to the end with no error message. One-letter words hide the bug because the first letter is also the last one.

bug อยู่ที่บรรทัด flipped += words[i][-pos] เพราะ pos เริ่มที่ 0 ทำให้ -pos คือ -0 ซึ่งเท่ากับ 0 รอบแรกจึงหยิบตัวแรกแทนตัวสุดท้าย แก้เป็น flipped += words[i][-(pos + 1)] เป็น logic error เพราะโปรแกรมรันจบโดยไม่มีข้อความ error ส่วนคำที่มีตัวอักษรตัวเดียวซ่อน bug ไว้ เพราะตัวแรกกับตัวสุดท้ายเป็นตัวเดียวกัน

Trace table of the buggy line: words[i] = "fun" trace table ของบรรทัดที่มี bug: words[i] = "fun"
pos-poswords[i][-pos]flipped
00'f'"f"
1-1'n'"fn"
2-2'u'"fnu"
[fixed]
[input]
[output]
Type a sentence: Python is fun
nohtyP si nuf

Example 2 — Swap lowercase and uppercase (swap_case) ตัวอย่างที่ 2 — สลับตัวพิมพ์เล็ก/ใหญ่ (swap_case)

1 The problem โจทย์

Read text that contains only English letters. If anything else is mixed in (spaces, digits, punctuation) or the text is empty, warn the user and ask again. Once the text is valid, show it with the case of every letter swapped: lowercase becomes uppercase and uppercase becomes lowercase.

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

  • Example: PyThOn → pYtHoN
  • only_letters(text) — returns False as soon as it meets a character that is not a letter, otherwise True (so empty text gives True; the main program checks for empty text itself)
  • flip_case(ch) — swaps the case of one letter
  • flip_text(text) — swaps the case of the whole text
  • ตัวอย่าง: PyThOn → pYtHoN
  • only_letters(text) — คืนค่า False ทันทีที่เจอตัวที่ไม่ใช่ตัวอักษร ถ้าไม่เจอเลยคืนค่า True (ข้อความว่างจึงได้ True โปรแกรมหลักต้องตรวจข้อความว่างเอง)
  • flip_case(ch) — สลับตัวพิมพ์ของตัวอักษร 1 ตัว
  • flip_text(text) — สลับตัวพิมพ์ทั้งข้อความ

New in this program: isalpha() is True when the character is a letter; len(word) > 0 and ... is True only when the text is not empty and the second check is also true; isupper() is True for a capital letter; upper() / lower() give the capital / small form; while True repeats forever until break leaves the loop.

คำสั่งใหม่ในโปรแกรมนี้: isalpha() ให้ค่า True เมื่อตัวนั้นเป็นตัวอักษร len(word) > 0 and ... เป็น True เมื่อข้อความไม่ว่างและเงื่อนไขหลัง and เป็นจริงด้วย isupper() ให้ค่า True เมื่อเป็นตัวพิมพ์ใหญ่ upper() / lower() แปลงเป็นตัวพิมพ์ใหญ่ / ตัวพิมพ์เล็ก และ while True วนซ้ำไปเรื่อยๆ จนกว่าจะเจอ break ซึ่งสั่งให้ออกจาก loop

2 Main program — flowchart and full code (contains a bug) โปรแกรมหลัก — flowchart และโค้ดทั้งหมด (มี bug)

The program starts a while True loop and reads text into word. It checks whether the text is not empty (len(word) > 0) and is letters only, using the sub-function only_letters: if both are true, break leaves the loop; if not, it shows a warning with print and goes back to ask again. After leaving the loop it calls flip_text(word), shows the result with print, and the program ends.

โปรแกรมนี้เริ่มต้นด้วยวน loop while True แล้วรับข้อความเก็บใน word ตรวจสอบเงื่อนไขว่าข้อความไม่ว่าง (len(word) > 0) และเป็นตัวอักษรล้วนด้วย function ย่อย only_letters หรือไม่ ถ้าใช่ทั้งสองข้อให้ break ออกจาก loop ถ้าไม่ใช่ให้แสดงข้อความเตือนด้วย print แล้วกลับไปรับค่าใหม่ หลังออกจาก loop จึงเรียก flip_text(word) แล้วแสดงผลด้วย print และจบการทำงานของโปรแกรม

Start ask until the text is valid Keep asking while True Yes Ask for the text word = input(...) Not empty and letters only? len(word) > 0 and only_letters(word)? No Warn the user print('Letters A-Z ...') Yes Leave the loop break Show the swapped text print(flip_text(word)) End
[main]
[input]
[output]
Type letters only: Py 3
Letters A-Z or a-z only, please try again
Type letters only: PyThOn
N

Try it yourself — the first value you submit clears the example and starts a new run. Submit one value at a time: text that is not letters only shows the warning and waits for the next value; valid text finishes the run. Submitting after that starts a new run.

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

3 only_letters(text)

only_letters starts by setting i = 0 as the character position. It checks whether i < len(text); if it is, it checks whether text[i].isalpha(). If that character is not a letter, it sends back False with return straight away. If it is a letter, it increases i by 1 and checks again. This repeats until i < len(text) is false, so every character was a letter and it sends back True with return. Empty text never enters the loop, so it also gives True.

only_letters เริ่มต้นด้วยกำหนดค่า i = 0 เป็นตัวชี้ตำแหน่งตัวอักษร แล้วตรวจสอบเงื่อนไขว่า i < len(text) หรือไม่ ถ้าใช่ให้ตรวจสอบว่า text[i].isalpha() หรือไม่ ถ้าตัวนั้นไม่ใช่ตัวอักษรให้ส่งค่า False กลับด้วย return ทันที ถ้าเป็นตัวอักษรให้เพิ่ม i ขึ้น 1 แล้วกลับไปตรวจสอบอีกครั้ง ทำแบบนี้จน i < len(text) เป็นเท็จ แสดงว่าทุกตัวเป็นตัวอักษร จึงส่งค่า True กลับด้วย return ส่วนข้อความว่างไม่เข้า loop เลย จึงได้ True เช่นกัน

only_letters(text) Start at the first character i = 0 More characters? i < len(text)? Yes Is it a letter? text[i].isalpha()? No return False Yes Next character i += 1 No return True

Test the function on its own before using it in the main program. Decide which kind of test case each value is.

ทดสอบฟังก์ชันนี้แยกเดี่ยวๆ ก่อนนำไปใช้ในโปรแกรมหลัก แล้วจัดว่าแต่ละค่าเป็น test case ชนิดไหน

[test]
[output]
True
False
True
False
True
Show the kinds of test case ดูชนิดของ test case
  • 'python', 'PyThOn' — Normal
  • 'Data Science' (has a space), 'abc123' — Error
  • '' (empty text) — Boundary. The function gives True because there is no character that is not a letter, so the main program rejects empty text with len(word) > 0.
  • 'python', 'PyThOn' — Normal
  • 'Data Science' (มีช่องว่าง), 'abc123' — Error
  • '' (ข้อความว่าง) — Boundary ฟังก์ชันให้ค่า True เพราะไม่มีตัวไหนที่ไม่ใช่ตัวอักษร โปรแกรมหลักจึงปฏิเสธข้อความว่างด้วย len(word) > 0
4 flip_text(text) & flip_case(ch)

flip_text starts by setting swapped = '' to hold the result and i = 0 as the letter position. It checks whether i < len(text); if it is, it swaps one letter with the sub-function flip_case, stores it in swapped, increases i by 1 and checks again. This repeats until the condition is false, so it sends swapped back with return.

flip_text เริ่มต้นด้วยกำหนดค่า swapped = '' สำหรับเก็บผลลัพธ์ และ i = 0 เป็นตัวชี้ตำแหน่งตัวอักษร แล้วตรวจสอบเงื่อนไขว่า i < len(text) หรือไม่ ถ้าใช่ให้สลับตัวพิมพ์ของตัวอักษร 1 ตัวด้วย function ย่อย flip_case แล้วเก็บไว้ใน swapped เพิ่ม i ขึ้น 1 แล้วกลับไปตรวจสอบอีกครั้ง ทำแบบนี้จนเงื่อนไขเป็นเท็จ จึงส่งค่า swapped กลับด้วย return

flip_case checks whether ch.isupper(); if it is, it sends back ch.lower() with return, otherwise it sends back ch.upper().

flip_case ตรวจสอบเงื่อนไขว่า ch.isupper() หรือไม่ ถ้าใช่ให้ส่งค่า ch.lower() กลับด้วย return ถ้าไม่ใช่ให้ส่งค่า ch.upper() กลับ

flip_text(text) Start an empty result swapped = '' Start at the first letter i = 0 More letters? i < len(text)? Yes Swap the case of one letter swapped = flip_case(text[i]) Next letter i += 1 No return swapped
flip_case(ch) Capital letter? ch.isupper()? Yes return ch.lower() No return ch.upper()
5 Test list รายการทดสอบ

Submit the values in order with the form in card 2, or put them one per line in the [input] box, then compare with the expected output column.

ส่งค่าตามลำดับด้วยช่องในการ์ดที่ 2 หรือใส่ไว้บรรทัดละค่าในกล่อง [input] แล้วเทียบกับคอลัมน์ผลที่ควรได้

Input (in order)ข้อมูลที่ป้อน (ตามลำดับ) Kindชนิด Expected outputผลที่ควรได้ Output of the buggy codeผลจากโค้ดที่มี bug
PyThOn Normal pYtHoN N ✗
helloWORLD Normal HELLOworld d ✗
a Boundary (one letter)Boundary (ตัวอักษรเดียว) A A ✓
(empty), then(ว่าง) แล้ว Ok Boundary (empty text)Boundary (ข้อความว่าง) 1 warning, thenเตือน 1 ครั้ง แล้ว oK 1 warning, thenเตือน 1 ครั้ง แล้ว K ✗
Py 3, abc123, Go Error 2 warnings, thenเตือน 2 ครั้ง แล้ว gO 2 warnings, thenเตือน 2 ครั้ง แล้ว O ✗
6 Your tasks ภารกิจ
  1. Check the code of all three functions and the main program against their flowcharts.
  2. Run the test list. The input check (warn, then ask again) works, but the swapped text is wrong. Which letter of the text is the one that is left?
  3. Fill in the trace table of swapped below for text = 'PyThOn'.
  4. Add print(i, swapped) inside the loop of flip_text to confirm where the bug is.
  5. Fix the code and run the whole test list again.
  6. Think further: why is testing with a alone not enough?
  1. ตรวจโค้ดทั้ง 3 ฟังก์ชันและส่วนหลักเทียบกับ flowchart
  2. รันรายการทดสอบ ส่วนตรวจข้อมูล (เตือนแล้วถามใหม่) ทำงานถูก แต่ผลสลับตัวพิมพ์ผิด ตัวอักษรที่เหลืออยู่คือตัวไหนของข้อความ
  3. กรอก trace table ของ swapped ด้านล่าง เมื่อ text = 'PyThOn'
  4. ใส่ print(i, swapped) ใน loop ของ flip_text เพื่อยืนยันตำแหน่ง bug
  5. แก้โค้ดแล้วรันรายการทดสอบซ้ำครบทุกข้อ
  6. ชวนคิด: ทำไมทดสอบด้วย a อย่างเดียวจึงไม่พอ

swapped (before) is the value of swapped at the end of each round with the original code, before your fix; fill in swapped (after) with its value at the end of each round after your fix.

swapped (before) คือค่าของ swapped หลังจบแต่ละรอบด้วยโค้ดเดิม ก่อนแก้ bug ส่วน swapped (after) ให้กรอกค่าของ swapped หลังจบแต่ละรอบหลังจากคุณแก้ bug แล้ว

Trace table: text = 'PyThOn' trace table: text = 'PyThOn'
itext[i]flip_case(text[i])swapped (before)swapped (after)
0    
1    
2    
3    
4    
5    
7 Solution เฉลย
Show the fixed code ดูโค้ดที่แก้แล้ว

The bug is on the line swapped = flip_case(text[i]). Using = overwrites the old value in every round, so only the last letter is left. Change it to swapped += flip_case(text[i]). It is a logic error. A one-letter text hides the bug, because the last letter is also the whole text.

bug อยู่ที่บรรทัด swapped = flip_case(text[i]) การใช้ = ทำให้แต่ละรอบเขียนทับค่าเดิม swapped จึงเหลือเฉพาะตัวสุดท้าย แก้เป็น swapped += flip_case(text[i]) เป็น logic error ส่วนข้อความที่มีตัวอักษรตัวเดียวซ่อน bug ไว้ เพราะตัวสุดท้ายก็คือข้อความทั้งหมด

In the table, swapped (before) is the value of swapped at the end of each round before the fix (swapped = flip_case(text[i])), and swapped (after) is its value after the fix (swapped += flip_case(text[i])). Before the fix, each round throws the old value away and keeps only the new letter; after the fix, each new letter is added to the end, e.g. 'p' + 'Y' = 'pY'.

ในตาราง swapped (before) คือค่าของ swapped หลังจบแต่ละรอบ ก่อนแก้ bug (swapped = flip_case(text[i])) ส่วน swapped (after) คือค่าของ swapped หลังจบแต่ละรอบหลังแก้ bug (swapped += flip_case(text[i])) ก่อนแก้ ทุกรอบจะทิ้งค่าเดิมแล้วเหลือแค่ตัวอักษรใหม่ตัวเดียว หลังแก้ ตัวอักษรใหม่จะต่อท้ายค่าเดิม เช่น 'p' + 'Y' = 'pY'

Trace table before and after the fix: text = 'PyThOn' trace table ก่อนและหลังแก้ bug: text = 'PyThOn'
itext[i]flip_case(text[i])swapped (before)swapped (after)
0'P''p''p''p'
1'y''Y''Y''pY'
2'T''t''t''pYt'
3'h''H''H''pYtH'
4'O''o''o''pYtHo'
5'n''N''N''pYtHoN'
[fixed]
[input]
[output]
Type letters only: Py 3
Letters A-Z or a-z only, please try again
Type letters only: PyThOn
pYtHoN

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 — Errors and Exceptions (syntax errors and run-time exceptions, section 2) (syntax error และ exception ระหว่างรัน ส่วนที่ 2)
  2. Python docs — String Methods (split, isalpha, isdigit, isupper, upper, lower, sections 3–4) (split, isalpha, isdigit, isupper, upper, lower ส่วนที่ 3–4)
  3. Smithsonian National Museum of American History — Log Book With Computer Bug (the 1947 moth story, section 1) (เรื่องผีเสื้อกลางคืนปี 1947 ส่วนที่ 1)
  4. Flowchart symbols follow 01 Basic → Flowchart Basic. สัญลักษณ์ของ flowchart อ้างอิงตาม 01 Basic → Flowchart Basic

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

A program runs to the end with no error message, but the answer is wrong. Which kind of error is it?โปรแกรมรันจนจบ ไม่มีข้อความ error แต่คำตอบผิด เป็น error ชนิดใด?

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

เฉลย / Answer: (B) Logic errorLogic error

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

  • (A) Syntax errorSyntax error
    Wrong: a syntax error stops every line from running.ผิด: syntax error ทำให้โปรแกรมไม่ทำงานเลยสักบรรทัด
  • (B) Logic errorLogic error
    Correct! A logic error gives no warning; you only see it by comparing the result with a known answer.ถูกต้อง! logic error ไม่มีข้อความเตือน ต้องเทียบผลกับคำตอบที่รู้อยู่แล้วจึงจะเห็น
  • (C) Run-time errorRun-time error
    Wrong: a run-time error stops the program halfway with an error message.ผิด: run-time error ทำให้โปรแกรมหยุดกลางทางพร้อมข้อความ error
  • (D) Not an error, because the program finishedไม่ใช่ error เพราะโปรแกรมรันจบ
    Wrong: a wrong answer means there is a bug, even if the program finished.ผิด: คำตอบผิดแปลว่ามี bug แม้โปรแกรมจะรันจบ

ข้อ 2

2 / 10

The line print("Hi" (missing the closing bracket) causes which kind of error?บรรทัด print("Hi" (ลืมวงเล็บปิด) ทำให้เกิด error ชนิดใด?

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

เฉลย / Answer: (A) Syntax errorSyntax error

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

  • (A) Syntax errorSyntax error
    Correct! It breaks Python's grammar, so it is found before the run ('(' was never closed).ถูกต้อง! ผิดกฎไวยากรณ์ Python จึงพบตั้งแต่ก่อนรัน ('(' was never closed)
  • (B) Logic errorLogic error
    Wrong: a logic error still runs to the end, but this line stops the program from running at all.ผิด: logic error รันได้จนจบ แต่บรรทัดนี้ทำให้โปรแกรมรันไม่ได้เลย
  • (C) Run-time errorRun-time error
    Wrong: a run-time error happens during the run, but an unclosed bracket is found before the run starts.ผิด: run-time error เกิดระหว่างรัน แต่วงเล็บที่ไม่ปิดถูกพบก่อนเริ่มรัน
  • (D) No error, it prints Hiไม่มี error พิมพ์ Hi ได้ปกติ
    Wrong: every opening bracket must be closed.ผิด: วงเล็บที่เปิดต้องปิดเสมอ

ข้อ 3

3 / 10

With nums = [1, 2, 3], what happens on print(nums[3])?nums = [1, 2, 3] แล้วสั่ง print(nums[3]) จะเกิดอะไรขึ้น?

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

เฉลย / Answer: (C) Run-time error (IndexError)Run-time error (IndexError)

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

  • (A) Syntax errorSyntax error
    Wrong: the line is grammatically correct.ผิด: บรรทัดนี้เขียนถูกไวยากรณ์
  • (B) Logic error, it prints a wrong valueLogic error พิมพ์ค่าผิด
    Wrong: Python prints no value; it stops with an error message.ผิด: Python ไม่พิมพ์ค่าใดเลย แต่หยุดพร้อมข้อความ error
  • (C) Run-time error (IndexError)Run-time error (IndexError)
    Correct! The list only has index 0–2, so the run stops with IndexError: list index out of range.ถูกต้อง! ลิสต์มีดัชนี 0–2 เท่านั้น จึงหยุดระหว่างรันด้วย IndexError: list index out of range
  • (D) It runs normally and prints 3รันได้ปกติ พิมพ์ 3
    Wrong: 3 is at index 2, not index 3.ผิด: 3 อยู่ที่ดัชนี 2 ไม่ใช่ดัชนี 3

ข้อ 4

4 / 10

The accepted score range is 0–100. Testing with 100 is which kind of test case?ช่วงคะแนนที่รับได้คือ 0–100 การทดสอบด้วยค่า 100 เป็น test case ชนิดใด?

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

เฉลย / Answer: (D) Boundary test caseBoundary test case

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

  • (A) Normal test caseNormal test case
    Wrong: a normal case is ordinary data well inside the range, such as 72.ผิด: normal คือค่าทั่วไปกลางช่วง เช่น 72
  • (B) Error test caseError test case
    Wrong: 100 is still a valid score.ผิด: 100 ยังเป็นคะแนนที่ถูกต้อง
  • (C) Run-time test caseRun-time test case
    Wrong: there is no such kind of test case; run-time is a kind of error.ผิด: ไม่มี test case ชนิดนี้ run-time เป็นชื่อชนิดของ error
  • (D) Boundary test caseBoundary test case
    Correct! 100 sits right on the upper edge of the accepted range.ถูกต้อง! 100 อยู่ตรงขอบบนของช่วงที่รับได้

ข้อ 5

5 / 10

Entering abc into check_score is which kind of test case?ป้อน abc ให้ฟังก์ชัน check_score เป็น test case ชนิดใด?

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

เฉลย / Answer: (A) Error test caseError test case

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

  • (A) Error test caseError test case
    Correct! It is data of the wrong kind; the program must reject it (Invalid) without stopping halfway.ถูกต้อง! เป็นข้อมูลผิดชนิด โปรแกรมต้องปฏิเสธ (Invalid) โดยไม่หยุดกลางทาง
  • (B) Normal test caseNormal test case
    Wrong: abc is not a score that users normally enter.ผิด: abc ไม่ใช่คะแนนที่ผู้ใช้ป้อนกันเป็นปกติ
  • (C) Boundary test caseBoundary test case
    Wrong: a boundary case sits at the edge of the range, such as 0, 50 or 100.ผิด: boundary คือค่าที่ขอบของช่วง เช่น 0, 50, 100
  • (D) No need to test it, users never type thatไม่ต้องทดสอบ เพราะผู้ใช้ไม่ป้อนแบบนี้
    Wrong: users can always mistype, so the program must cope with it.ผิด: ผู้ใช้อาจพิมพ์ผิดได้เสมอ โปรแกรมต้องรับมือได้

ข้อ 6

6 / 10

Entering 72 into check_score is which kind of test case?ป้อน 72 ให้ฟังก์ชัน check_score เป็น test case ชนิดใด?

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

เฉลย / Answer: (B) Normal test caseNormal test case

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

  • (A) Boundary test caseBoundary test case
    Wrong: 72 is not at an edge of the range (0, 49/50, 100).ผิด: 72 ไม่ได้อยู่ที่ขอบของช่วง (0, 49/50, 100)
  • (B) Normal test caseNormal test case
    Correct! It is an ordinary score inside the range and checks the main job of the program.ถูกต้อง! เป็นคะแนนทั่วไปกลางช่วง ใช้ตรวจงานหลักของโปรแกรม
  • (C) Error test caseError test case
    Wrong: 72 is a valid score.ผิด: 72 เป็นคะแนนที่ถูกต้อง
  • (D) Syntax test caseSyntax test case
    Wrong: there is no such kind of test case; syntax is a kind of error.ผิด: ไม่มี test case ชนิดนี้ syntax เป็นชื่อชนิดของ error

ข้อ 7

7 / 10

Which statement describes debugging?ข้อใดอธิบาย debugging ได้ถูกต้อง?

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

เฉลย / Answer: (C) Finding which line the bug is on and what causes it, then fixing itหาว่า bug อยู่บรรทัดไหน เกิดจากอะไร แล้วแก้ให้ถูก

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

  • (A) Running the program with data whose answer is known, to see whether there is a bugรันโปรแกรมด้วยข้อมูลที่รู้คำตอบอยู่แล้ว เพื่อดูว่ามี bug หรือไม่
    Wrong: that is testing, which only tells you that something is wrong.ผิด: นี่คือ testing ซึ่งบอกแค่ว่ามีปัญหา
  • (B) Rewriting the whole program every time a bug appearsเขียนโปรแกรมใหม่ทั้งหมดทุกครั้งที่เจอ bug
    Wrong: debugging finds and fixes only the wrong spot.ผิด: debugging หาและแก้เฉพาะจุดที่ผิด
  • (C) Finding which line the bug is on and what causes it, then fixing itหาว่า bug อยู่บรรทัดไหน เกิดจากอะไร แล้วแก้ให้ถูก
    Correct! Debugging answers where the problem is, then fixes it.ถูกต้อง! debugging ตอบว่าปัญหาอยู่ตรงไหน แล้วแก้ไข
  • (D) Adding a comment to every lineเพิ่ม comment ให้ทุกบรรทัด
    Wrong: comments explain code but do not find or fix bugs.ผิด: comment ช่วยอธิบายโค้ด แต่ไม่ได้หาหรือแก้ bug

ข้อ 8

8 / 10

What is the value of "fun"[-0]?"fun"[-0] ให้ค่าใด?

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

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

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

  • (A) 'n''n'
    Wrong: 'n' is [-1].ผิด: 'n' คือ [-1]
  • (B) 'u''u'
    Wrong: 'u' is [1] or [-2].ผิด: 'u' คือ [1] หรือ [-2]
  • (C) IndexErrorIndexError
    Wrong: index 0 exists, so there is no error.ผิด: ดัชนี 0 มีอยู่จริง จึงไม่เกิด error
  • (D) 'f''f'
    Correct! -0 equals 0, so it gives the first letter. This is the bug in Example 1.ถูกต้อง! -0 เท่ากับ 0 จึงได้ตัวแรก นี่คือ bug ของตัวอย่างที่ 1

ข้อ 9

9 / 10

Which input gives the right result on the buggy flip_words?ข้อมูลใดทดสอบ flip_words ที่ยังมี bug แล้วได้ผลถูก?

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

เฉลย / Answer: (A) a Ia I

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

  • (A) a Ia I
    Correct! In a one-letter word the first letter is also the last, so the bug shows no sign.ถูกต้อง! คำที่มีตัวอักษรตัวเดียว ตัวแรกกับตัวสุดท้ายเป็นตัวเดียวกัน bug จึงไม่แสดงอาการ
  • (B) Python is funPython is fun
    Wrong: it gives Pnohty is fnu instead of nohtyP si nuf.ผิด: ได้ Pnohty is fnu แทน nohtyP si nuf
  • (C) Debug every loopDebug every loop
    Wrong: it gives Dgube eyrev lpoo instead of gubeD yreve pool.ผิด: ได้ Dgube eyrev lpoo แทน gubeD yreve pool
  • (D) Hi thereHi there
    Wrong: it gives Hi tereh instead of iH ereht.ผิด: ได้ Hi tereh แทน iH ereht

ข้อ 10

10 / 10

In flip_text, what happens if the loop says swapped = flip_case(text[i])?ใน flip_text ถ้าเขียน swapped = flip_case(text[i]) ใน loop จะได้ผลอย่างไร?

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

เฉลย / Answer: (B) Only the last letter, swapped, is leftเหลือเฉพาะตัวอักษรตัวสุดท้ายที่สลับแล้ว

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

  • (A) Every letter of the text is swappedได้ข้อความที่สลับตัวพิมพ์ครบทุกตัว
    Wrong: it needs += to join the result of every letter.ผิด: ต้องใช้ += จึงจะต่อผลของทุกตัว
  • (B) Only the last letter, swapped, is leftเหลือเฉพาะตัวอักษรตัวสุดท้ายที่สลับแล้ว
    Correct! = overwrites the old value every round, so only the last round's result is left.ถูกต้อง! = เขียนทับค่าเดิมทุกรอบ จึงเหลือผลของรอบสุดท้าย
  • (C) A run-time error occursเกิด run-time error
    Wrong: the program runs to the end without an error; it is a logic error.ผิด: โปรแกรมรันจบโดยไม่มี error เป็น logic error
  • (D) An empty textได้ข้อความว่าง
    Wrong: the last round leaves one letter in swapped.ผิด: รอบสุดท้ายเก็บตัวอักษร 1 ตัวไว้ใน swapped