Notes & software courses · Free to learn
Aph's Blog
On this page

วันที่ 15 — ประเภทข้อผิดพลาด (Python Type Errors)

👋 อ่านฟรีทั้งหมดบน Aph's Blog — เนื้อหาภาษาไทย ทำตามทีละหน้าใน sidebar ได้เลย หากมีข้อเสนอแนะหรืออยากให้เพิ่มหัวข้อไหน บอกได้เสมอ

เรียนรู้ประเภทข้อผิดพลาดที่พบบ่อยใน Python ตั้งแต่ SyntaxError จนถึง ZeroDivisionError พร้อมวิธีแก้ไขแต่ละประเภท

ประเภทข้อผิดพลาดใน Python

เมื่อเราเขียนโค้ดเป็นเรื่องปกติที่จะพิมพ์ผิดหรือเกิดข้อผิดพลาดอื่น ๆ ถ้าโค้ดรันไม่ได้ Python interpreter จะแสดงข้อความแจ้งตำแหน่งที่เกิดปัญหาและประเภทของข้อผิดพลาด บางครั้งยังแนะนำวิธีแก้ไขด้วย การเข้าใจประเภทข้อผิดพลาดต่าง ๆ จะช่วยให้ debug โค้ดได้เร็วขึ้นและเป็นโปรแกรมเมอร์ที่ดีขึ้น

SyntaxError

เกิดขึ้นเมื่อ syntax ไม่ถูกต้อง เช่น ลืมใส่วงเล็บ:

python
>>> 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 แนะนำวิธีแก้ไขให้ด้วย แก้ได้ดังนี้:

python
>>> print('hello world')
hello world

NameError

เกิดขึ้นเมื่อใช้ชื่อตัวแปรหรือ function ที่ยังไม่ได้ประกาศ:

python
>>> print(age)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'age' is not defined

ตัวแปร age ยังไม่ได้ประกาศ แก้ได้โดยประกาศและกำหนดค่าก่อน:

python
>>> age = 25
>>> print(age)
25

IndexError

เกิดขึ้นเมื่อใช้ index ที่เกินขอบเขตของ list:

python
>>> numbers = [1, 2, 3, 4, 5]
>>> numbers[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

List นี้มี index แค่ 0 ถึง 4 การใช้ index 5 จึงเกิน range

ModuleNotFoundError

เกิดขึ้นเมื่อ import module ที่ไม่มีอยู่หรือพิมพ์ชื่อผิด:

python
>>> import maths
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'maths'

พิมพ์ชื่อ module ผิด มี s ต่อท้ายที่ไม่ควรมี แก้โดยลบ s ออก:

python
>>> import math
>>>

AttributeError

เกิดขึ้นเมื่อเรียกใช้ attribute หรือ method ที่ไม่มีอยู่ใน object นั้น:

python
>>> 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 แก้ได้ดังนี้:

python
>>> math.pi
3.141592653589793

KeyError

เกิดขึ้นเมื่อใช้ key ที่ไม่มีอยู่ใน dictionary:

python
>>> 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' แก้ดังนี้:

python
>>> users['country']
'Finland'

TypeError

เกิดขึ้นเมื่อใช้ operation กับ data type ที่ไม่รองรับ:

python
>>> 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 ก่อน:

python
>>> 4 + int('3')
7
>>> 4 + float('3')
7.0

ImportError

เกิดขึ้นเมื่อ import function ที่ไม่มีอยู่ใน module นั้น:

python
>>> 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 แก้ดังนี้:

python
>>> from math import pow
>>> pow(2,3)
8.0

ValueError

เกิดขึ้นเมื่อ function ได้รับ argument ที่มี type ถูกต้องแต่ค่าไม่เหมาะสม:

python
>>> 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

เกิดขึ้นเมื่อหารด้วยศูนย์:

python
>>> 1/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero

ไม่สามารถหารตัวเลขด้วยศูนย์ได้

💻 แบบฝึกหัด — วันที่ 15

  1. เปิด Python interactive shell แล้วลองรันตัวอย่างทั้งหมดที่เรียนมาในบทนี้