On this page
วันที่ 15 — ประเภทข้อผิดพลาด (Python Type Errors)
เรียนรู้ประเภทข้อผิดพลาดที่พบบ่อยใน Python ตั้งแต่ SyntaxError จนถึง ZeroDivisionError พร้อมวิธีแก้ไขแต่ละประเภท
ประเภทข้อผิดพลาดใน Python
เมื่อเราเขียนโค้ดเป็นเรื่องปกติที่จะพิมพ์ผิดหรือเกิดข้อผิดพลาดอื่น ๆ ถ้าโค้ดรันไม่ได้ Python interpreter จะแสดงข้อความแจ้งตำแหน่งที่เกิดปัญหาและประเภทของข้อผิดพลาด บางครั้งยังแนะนำวิธีแก้ไขด้วย การเข้าใจประเภทข้อผิดพลาดต่าง ๆ จะช่วยให้ debug โค้ดได้เร็วขึ้นและเป็นโปรแกรมเมอร์ที่ดีขึ้น
SyntaxError
เกิดขึ้นเมื่อ syntax ไม่ถูกต้อง เช่น ลืมใส่วงเล็บ:
>>> print 'hello world'
File "<stdin>", line 1
print 'hello world'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hello world')?ลืมใส่ parenthesis รอบ string Python แนะนำวิธีแก้ไขให้ด้วย แก้ได้ดังนี้:
>>> print('hello world')
hello worldNameError
เกิดขึ้นเมื่อใช้ชื่อตัวแปรหรือ function ที่ยังไม่ได้ประกาศ:
>>> print(age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'age' is not definedตัวแปร age ยังไม่ได้ประกาศ แก้ได้โดยประกาศและกำหนดค่าก่อน:
>>> age = 25
>>> print(age)
25IndexError
เกิดขึ้นเมื่อใช้ index ที่เกินขอบเขตของ list:
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of rangeList นี้มี index แค่ 0 ถึง 4 การใช้ index 5 จึงเกิน range
ModuleNotFoundError
เกิดขึ้นเมื่อ import module ที่ไม่มีอยู่หรือพิมพ์ชื่อผิด:
>>> import maths
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'พิมพ์ชื่อ module ผิด มี s ต่อท้ายที่ไม่ควรมี แก้โดยลบ s ออก:
>>> import math
>>>AttributeError
เกิดขึ้นเมื่อเรียกใช้ attribute หรือ method ที่ไม่มีอยู่ใน object นั้น:
>>> import math
>>> math.PI
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'PI'ค่าคงที่ pi ใน math module เขียนเป็นตัวเล็กว่า pi ไม่ใช่ PI แก้ได้ดังนี้:
>>> math.pi
3.141592653589793KeyError
เกิดขึ้นเมื่อใช้ key ที่ไม่มีอยู่ใน dictionary:
>>> users = {'name':'Asab', 'age':250, 'country':'Finland'}
>>> users['name']
'Asab'
>>> users['county']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'county'พิมพ์ key ผิด 'county' ควรเป็น 'country' แก้ดังนี้:
>>> users['country']
'Finland'TypeError
เกิดขึ้นเมื่อใช้ operation กับ data type ที่ไม่รองรับ:
>>> 4 + '3'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'ไม่สามารถบวก number กับ string ได้โดยตรง วิธีแก้คือแปลง string เป็น int หรือ float ก่อน:
>>> 4 + int('3')
7
>>> 4 + float('3')
7.0ImportError
เกิดขึ้นเมื่อ import function ที่ไม่มีอยู่ใน module นั้น:
>>> from math import power
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'power' from 'math'ไม่มี function ชื่อ power ใน math module ชื่อที่ถูกต้องคือ pow แก้ดังนี้:
>>> from math import pow
>>> pow(2,3)
8.0ValueError
เกิดขึ้นเมื่อ function ได้รับ argument ที่มี type ถูกต้องแต่ค่าไม่เหมาะสม:
>>> int('12a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12a'ไม่สามารถแปลง string '12a' เป็น integer ได้เพราะมีตัวอักษร 'a' ปนอยู่
ZeroDivisionError
เกิดขึ้นเมื่อหารด้วยศูนย์:
>>> 1/0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zeroไม่สามารถหารตัวเลขด้วยศูนย์ได้
💻 แบบฝึกหัด — วันที่ 15
- เปิด Python interactive shell แล้วลองรันตัวอย่างทั้งหมดที่เรียนมาในบทนี้