Lists ใช้ index numbers ในการเข้าถึงสมาชิก ซึ่งเป็นค่า integer หากพยายามเข้าถึงสมาชิกใน list ด้วย floating-point number จะเกิดข้อผิดพลาด "TypeError: list indices must be integers or slices, not float"
คู่มือนี้อธิบายความหมายและสาเหตุ พร้อมตัวอย่างการแก้ไข
ทำความเข้าใจ TypeError: list indices must be integers or slices, not float
เมื่อเพิ่มสมาชิกเข้า list สมาชิกแต่ละตัวจะได้รับค่า index เริ่มจาก 0 และเพิ่มทีละ 1 สำหรับสมาชิกใหม่ ทำให้เข้าถึงสมาชิกแต่ละตัวได้ง่าย สมาชิกตัวแรก index 0 ตัวที่สอง index 1 ตามลำดับ
ไม่สามารถดึงสมาชิกจาก list ด้วย floating-point numbers ได้ เพราะ floating-point numbers เป็นชนิดข้อมูลต่างกัน Python จัดการต่างกัน Lists คาดหวัง integer index เนื่องจาก lists ถูก index ด้วย integers
ตัวอย่างสถานการณ์
เราจะสร้างโปรแกรมดึงข้อมูลผู้เข้าร่วมการแข่งขันเทนนิส เริ่มต้นด้วยการกำหนด list of lists ที่เก็บข้อมูลผู้เข้าร่วมทั้งหมด:
participants = [
["Alex Rogers", 5, 2],
["Linda Patterson", 3, 4],
["Ruby Spencer", 1, 6]
]
List participants มี 3 sublists แต่ละ sublist เก็บชื่อผู้เข้าร่วม, จำนวนเกมที่ชนะ, จำนวนเกมที่แพ้ ตามลำดับ
ค่าจัดเรียงตามจำนวนครั้งที่ชนะ
ต่อมา ให้ผู้ใช้ป้อนตำแหน่งของผู้เล่นที่ต้องการดูข้อมูล ใช้ input():
player_to_find = float(input("Enter the leaderboard position of the player you want to find: "))
ตัวเลขที่ผู้ใช้ป้อนควรสอดคล้องกับตำแหน่งบน leaderboard Alex Rogers อยู่อันดับ 1 หากต้องการดูข้อมูลเขา ให้ป้อน 1
เราแปลงค่าที่ป้อนเป็น float เพราะคิดว่าจะใช้ indexing ต่อไป Index values เป็นตัวเลข จึงต้องการตัวเลข
ต่อมา ใช้ indexing ดึงข้อมูลผู้เล่นที่ต้องการ:
player_info = participants[player_to_find - 1]
เราใช้ค่าที่ป้อนเพื่อหา record ของผู้เล่นจาก list "participants" เราลบ 1 จาก "player_to_find" เพราะ lists index เริ่มจาก 0 แต่อันดับแรกบน leaderboard คือ #1
ต่อมา พิมพ์รายละเอียดผู้เข้าร่วม:
print("Name: {}".format(player_info[0]))
print("Wins: {}".format(player_info[1]))
print("Losses: {}".format(player_info[2]))
แสดงชื่อ, จำนวนเกมที่ชนะ, จำนวนเกมที่แพ้ ลองรันโปรแกรม:
Enter the leaderboard position of the player you want to find: 2 Traceback (most recent call last): File "main.py", line 9, inplayer_info = participants[player_to_find + 1] TypeError: list indices must be integers or slices, not float
หลังป้อนตำแหน่งแล้ว เกิดข้อผิดพลาด
วิธีแก้ไข
เราแปลงค่าที่ป้อนเป็น float ด้วย method float() นี่คือข้อผิดพลาด เพราะตอนจะดึงสมาชิกจาก list "participants" เราพยายามใช้ floating-point number ทำให้เกิด error
เพื่อแก้ไข ต้องแปลงค่าที่ป้อนเป็น integer แทน floating-point number ใช้ method int():
player_to_find = int(input("Enter the leaderboard position of the player you want to find: "))
ตอนนี้เรามีค่า integer สามารถดึงสมาชิกจาก lists ด้วย indexing ได้ ลองรันใหม่:
Enter the number of the player you want to find: 2 Name: Linda Patterson Wins: 3 Losses: 4
โค้ดทำงานสำเร็จ แสดงข้อมูลผู้เล่นอันดับ 2
สรุป
ข้อผิดพลาด "TypeError: list indices must be integers or slices, not float" เกิดเมื่อพยายามเข้าถึงสมาชิกใน list ด้วย floating-point number วิธีแก้คือใช้ integers เท่านั้นในการเข้าถึงสมาชิกใน list ผ่าน index value
ตอนนี้คุณพร้อมแก้ไขข้อผิดพลาดนี้ในโค้ดของตัวเองอย่างมืออาชีพแล้ว!