Devdit
 

AttributeError: int object has no attribute sort Python คือ วิธีแก้ไข

2.4K

รันโค้ด Python แล้วขึ้น Error AttributeError: 'int' object has no attribute sort Python เขียนโค้ดตามด้านล่าง ต้องแก้ไขอย่างไร

>>> a = 50
>>> a.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'sort'

 

วิธีแก้ไข

คำสั่ง sort() ใช้สำหรับตัวแปรที่รองรับการเรียงลำดับข้อมูล เช่น List ไม่สามารถเรียงลำดับตัวแปรชนิด int ได้ ตัวอย่างการใช้คำสั่ง sort()

a = [10,20,30,40,50]
a.sort()
print(a)
แก้ไข 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ