Skip to content · ⁨Bỏ qua nội dung⁩

Find the position · ⁨Tìm vị trí⁩

English

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.

Tiếng Việt

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.

Log in or create account · ⁨Đăng nhập hoặc tạo tài khoản⁩

IGCSE, A-Level & AP