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 开始计数。
用 range(len(items)) 遍历位置,这样你既有下标 i,又有元素 items[i]。return 会立刻离开函数,这正是找到第一个匹配时需要的。只有在循环检查完所有元素之后,才返回 -1。
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 第一次出现的位置;如果不存在,返回 -1。不能使用 .index()。
Click Run to see the output here. · 点击“运行”查看此处输出。