Errors & testing
Three kinds of error
- A syntax error breaks the rules of Python, so the code will not run.
- A runtime error crashes the program while it is running.
- A logic error runs fine but gives the wrong answer.
ประเภทของข้อผิดพลาดสามชนิด
- Syntax error (ข้อผิดพลาดทางไวยากรณ์): ขัดกฎของ Python ทำให้โค้ดรันไม่ได้
- Runtime error (ข้อผิดพลาดขณะรัน): โปรแกรมพังระหว่างที่กำลังทำงานอยู่
- Logic error (ข้อผิดพลาดทางตรรกะ): รันได้ปกติแต่ให้คำตอบที่ ผิด
Syntax errors
- These are typos: a missing
:, an unclosed quote, a wrong indent. - Python refuses to start and shows you the line.
- Fix the spelling or punctuation, then run again.
Syntax errors
- เป็นความผิดพลาดในการพิมพ์: ขาด
:, มีเครื่องหมายอรรถกรรมที่ไม่ปิด, หรือมีการจัดย่อหน้าผิด - Python จะปฏิเสธการเริ่มต้นและแสดงบรรทัดที่มีปัญหาให้คุณดู
- แก้ไขคำหรือเครื่องหมายอรรถกรรม แล้วลองรันใหม่อีกครั้ง
Runtime errors
- The code starts, then hits something it cannot do.
- Examples:
10 / 0, orint("hello"), or reading past the end of a list. - The program stops at that line with a message.
Runtime errors
- โค้ดเริ่มทำงานแล้วไปเจอสิ่งที่มันไม่สามารถทำได้
- ตัวอย่างเช่น:
10 / 0, หรือint("hello"), หรือการอ่านข้อมูลเกินขอบเขตของลิสต์ - โปรแกรมจะหยุดที่บรรทัดนั้นพร้อมแสดงข้อความแจ้งเตือน
Logic errors
- The program runs to the end but the result is wrong.
- Example: using
-where you meant+, or stopping a loop too early. - These are the hardest to find — the computer did exactly what you wrote.
Logic errors
- โปรแกรมรันจนจบแต่ผลลัพธ์ที่ได้ไม่ถูกต้อง
- ตัวอย่าง: ใช้
-แทนที่จะใช้+, หรือหยุดรอบการทำซ้ำก่อนกำหนด - นี่คือประเภทที่หาได้ยากที่สุด — คอมพิวเตอร์ทำตามที่คุณเขียนมาเป๊ะๆ เลย
Read the traceback
- When a program crashes, Python prints a traceback.
- Read the last line first: it names the error and what went wrong.
- The line number tells you where to look.
อ่าน traceback
- เมื่อโปรแกรมพัง Python จะพิมพ์ traceback ออกมา
- อ่านบรรทัด สุดท้าย ก่อน: มันจะบอกชื่อข้อผิดพลาดและสาเหตุ
- หมายเลขบรรทัดบอกตำแหน่งที่คุณควรไปตรวจสอบ
Traceback (most recent call last):
File "<program>", line 2, in <module>
print(10 / 0)
ZeroDivisionError: division by zero
Test with chosen data
- Do not trust code until you have tested it.
- Use normal data, boundary data (like
0), and extreme data. - Check each answer against what you worked out by hand.
ทดสอบด้วยข้อมูลที่เลือก
- อย่าเชื่อใจโค้ดจนกว่าคุณจะได้ ทดสอบ แล้ว
- ใช้ ข้อมูลปกติ, ข้อมูลขอบเขต (เช่น
0), และ ข้อมูลสุดขั้ว。 - ตรวจสอบคำตอบแต่ละข้อเทียบกับผลที่คุณคำนวณเองบนกระดาษ
def square(n):
return n * n
print(square(3)) # normal -> 9
print(square(0)) # boundary -> 0
print(square(-2)) # negative -> 4
Common mistakes
- A syntax error stops the program before it runs, so fix those first.
- Catch a specific error with
except ValueError:, not a bareexcept:. - Test the tricky cases (empty, zero, the biggest), not just the easy one.
ข้อผิดพลาดที่พบบ่อย
- Syntax error จะหยุดโปรแกรมก่อนรัน ดังนั้นต้องแก้จุดนี้ก่อนเสมอ
- จับเฉพาะข้อผิดพลาดที่ต้องการด้วย
except ValueError:, ไม่ใช่ bareexcept:ทั่วไป - ทดสอบเคสที่ยาก (ว่าง,ศูนย์,ค่ามากที่สุด) ไม่เพียงแต่เคสที่ง่าย
Now you try
- Each task has a logic error to fix, or a crash to prevent.
- Press Check answer to test your fix against several cases.
ลองดูเลย
- แต่ละโจทย์จะมี logic error ให้แก้ หรือมี crash ที่ต้องป้องกัน
- กด Check answer เพื่อทดสอบการแก้ไขของคุณกับหลายกรณี
Find and fix a bug
Testing is a cycle: run it, see what's wrong, fix, run again. · การทดสอบคือ วงจร: รันดู, ดูว่าผิดตรงไหน, แก้ไข, รันอีกครั้ง
This function should add a and b, but it has a logic error — it subtracts. Fix it so it returns the sum. · ฟังก์ชันนี้ควร บวก a และ b, แต่มันมีข้อผิดพลาดทางตรรกะ — มันกำลังลบอยู่. แก้ไขให้มันคืนค่าผลบวกแทน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
sum_to(n) should return 1 + 2 + ... + n, but it stops one number too early (an off-by-one logic error). Fix the range so it includes n. · sum_to(n) ควรคืนค่า 1 + 2 + ... + n, แต่มันหยุดเร็วเกินไปหนึ่งรอบ (ข้อผิดพลาด off-by-one). แก้ไข range เพื่อให้รวม n ด้วย
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write safe_divide(a, b) that returns a / b, but prevents a runtime crash: if b is 0, return 0 instead. · เขียน safe_divide(a, b) ที่คืนค่า a / b, แต่ ป้องกัน การเกิด runtime error: ถ้า b เป็น 0, ให้คืนค่า 0 แทน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
average(nums) has two bugs: it divides by the wrong number, and it crashes on an empty list. Fix both, so average([2, 4, 6]) is 4 and average([]) is 0. · average(nums) มี สอง ความผิดพลาด: หารด้วยตัวเลขผิด และเกิดข้อผิดพลาดเมื่อได้รับรายการว่าง. แก้ไขทั้งสองอย่าง เพื่อให้ average([2, 4, 6]) เป็น 4 และ average([]) เป็น 0.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่