Find the position · Tìm vị trí
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.
Tìm vị trí
Tìm kiếm trong danh sách và báo vị trí của một thứ gì đó, đếm từ 0.
Duyệt qua các vị trí bằng range(len(items)), để bạn có chỉ số i cũng như phần tử items[i]. Hàm return sẽ thoát khỏi hàm ngay lập tức,这正是你所期望的在第一次匹配时。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(). · Viết find_index(items, target) trả về vị trí của target đầu tiên trong items, hoặc -1 nếu không tìm thấy. Không sử dụng .index().
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.