Find the position · หาตำแหน่ง
Find the position
Search a list and report where something is, counting from 0.
Loop over the positions with range(len(items)), so you have the index i as well as the item items[i]. return leaves the function straight away, which is exactly what you want at the first match. Return -1 only after the loop has checked every item.
หาตำแหน่ง
ค้นหาในลิสต์และรายงาน ตำแหน่ง ที่พบสิ่งนั้น โดยนับเริ่มจาก 0
Loop over the positions with range(len(items)), so you have the index i as well as the item items[i]. return leaves the function straight away, which is exactly what you want at the first match. Return -1 only after the loop has checked every item.
Write find_index(items, target) that returns the position of the first target in items, or -1 if it is not there. Do not use .index(). · เขียน find_index(items, target) ที่ส่งกลับตำแหน่งของ target ตัวแรกใน items หรือส่งกลับ -1 หากไม่พบ อย่าใช้ .index()
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่